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

10 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

dpb
dpb 2013-8-9
>> 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 个评论
Leon
Leon 2013-8-9
编辑:Leon 2013-8-9
Thank you so much, Dpb. That solved my problem. I appreciate your time in helping me out very much!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by