Resolved: Need help for using textscan to read a csv file

11 Ansichten (letzte 30 Tage)
Leon
Leon am 8 Aug. 2013
Below is my code. I could not find any issues with that. But the results "tmp" are always empty {0x70 cell}.
Please help. Thanks.
====== Code =======
fid = fopen('testfile.csv', 'rt');
formatspec = [repmat('%s ', 1, 70) '%*[^\n]'];
tmp = textscan(fid, formatspec, 1, 'delimiter', ',', 'collectoutput', true, 'headerlines', 16);
fclose(fid);
======= One row of the data file ========== 09FA20010524,09FA20010524,1,2.00105E+17,1,24,2,20010525,0,-44.429,179.9582,1011,989,1000.8,4.531,-999,9,34.307,2,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,2.781,2,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,9,-999,-999,-999,-999,-999,4.45259,27.1871,31.7778,36.2656,40.6526,44.9407
  4 Kommentare
Walter Roberson
Walter Roberson am 9 Aug. 2013
Could you show us a sample line? The 17th line from the csv ?
Leon
Leon am 9 Aug. 2013
I just added a sample line as above. Thanks.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 9 Aug. 2013
>> s='09FA20010524,09FA20010524,...,31.7778,36.2656,40.6526,44.9407';
>> fmt = [repmat('%s ', 1, 70) '%*[^\n]'];
>> textscan(s, fmt, 1, 'delimiter', ',', 'collectoutput', true)
ans =
{1x70 cell}
>> ans{1}
ans =
Columns 1 through 7
'09FA20010524' '09FA20010524' '1' '2.00105E+17' '1' '24' '2'
Columns 8 through 14
'20010525' '0' '-44.429' '179.9582' '1011' '989' '1000.8'
...
Columns 68 through 70
'44.9407' [] []
>> length(findstr(s,','))
ans =
67
Your problem is the last -- there are only 68 fields, not 70 at least in the above record and that combined w/ the last '%*[^\n]' in the format string causes the failure on subsequent lines. Also, lose the extra blank in the format string; it instructs textscan() to try to match an explicit blank of which there are none in the file--I'm a little surprised that didn't cause a failure.
Just use
fmt = repmat('%s', 1, 68);
should be all you need.
Check the input file for consistency in number of columns/record, though, too.
  1 Kommentar
Leon
Leon am 9 Aug. 2013
Bearbeitet: Leon am 9 Aug. 2013
Thank you so much, Dpb. That solved my problem. I appreciate your time in helping me out very much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by