1

I have a textfile formatted like this:

3,4, ,CMX COP,JUL11,ABCD4,APPM,CME,4PTS09,F,HG,27, , ,1,4.3,2,27,23,1,NCAP1,0
5,6, ,AUD,JUN11,ABCD4,APPM,CME,4PTS09,F,6A,11, , ,1,1.7,10,27,23,1,NCAP1,0

I'm trying to read it with textscan, but I don't know how to deal with the blank spaces. I tried:

filename = 'E:\20110427.csv';

fileid = fopen(filename,'rt');
Data = textscan(fileid, '%d,%d,%c,%s,%s,%s,%s,%s,%s,%s,%s,%d,%c,%c,%d,%f,%d,%d,%d,%d,%s,%d')

The space between CMX and COP seems to be throwing it off, as well as potentially the blanks between the commas. Any suggestions?

CMX COP is one thing. It would be okay to delete the space.

siegel
  • 819
  • 2
  • 12
  • 24

3 Answers3

1

You need to use the 'Delimiter' option for textscan (I also changed the %c on the spaces to %s to avoid reading the delimiter).

str = '5,6, ,AUD,JUN11,ABCD4,APPM,CME,4PTS09,F,6A,11, , ,1,1.7,10,27,23,1,NCAP1,0';
Data = textscan(str, '%d%d%s%s%s%s%s%s%s%s%s%d%s%s%d%f%d%d%d%d%s%d', 'Delimiter', ',');
ioums
  • 1,367
  • 14
  • 20
  • How do I make this work for every line of the file? This works correctly for reading just the first line. – siegel Aug 15 '13 at 19:17
  • I don't see why you can only read the first line. If I put your two lines into a csv file and use my method with the file Id I get Data being a 1x22 cell where each cell has values from each row of the file. – ioums Aug 15 '13 at 19:26
  • Got it. I made an error, having previously defined str to be only the first line. Thank you! – siegel Aug 15 '13 at 19:30
1

Or you can use the function regexp :

A = '3,4, ,CMX COP,JUL11,ABCD4,APPM,CME,4PTS09,F,HG,27, , ,1,4.3,2,27,23,1,NCAP1,0';
regexp(A,'\w','match')
m_power
  • 3,156
  • 5
  • 33
  • 54
0

If the same values are always missing on each lien you could just tell matlab to use multiple delimiters as one:

C = textscan(fileID,'%f %f %f %f','Delimiter',',','MultipleDelimsAsOne',1);

where 1 means true.

jpmorr
  • 500
  • 5
  • 25