フィルターのクリア

how to let 'textscan' stop when read an empty line

8 ビュー (過去 30 日間)
Ken
Ken 2012 年 7 月 24 日
I have double quoted csv file:
"header1"
(descriptions)
"A","B","3","A","B","3"
"C","B","3","T","B","2"
"D","B","3","A","H","3"
"E","B","3","A","B","6"
"F","B","3","Z","J","3"
(empty line here)
"header2"
(descriptions)
"A","B","3","A","B","3"
"C","B","5","A","B","3"
"D","B","4","A","H","3"
"E","B","3","A","L","1"
"F","B","2","A","I","3"
I tried to use:
fid=fopen('..\data.csv', 'r')
fgetl(fid)
fgetl(fid)
inputtext = textscan(fid, '%q%q%q%q%q%q', ...
'delimiter', ',');
but it reads everything until hits the end of the data file (including header2). How to read data above the empty line? Thanks!!

採用された回答

Ken
Ken 2012 年 7 月 24 日
編集済み: Ken 2012 年 7 月 24 日
instead of reading the whole block of data using textscan, I tried to read it into a cell line by line. Textscan will stop when hits the empty line.
Code:
while (~feof(fid_t))
temp1 = fgetl(fid_t);
if isempty(temp1)
break;
end
inputtext=textscan(temp1, formatstring,...
'delimiter',',')
temp_out=[inputtext{:}];
Data(end+1,:)=temp_out;
end
and it works finally. It is still not an effective way, but it works. any suggestion?

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 7 月 24 日
textscan() is not designed for this.
You can remove newline from the Whitespace list, but to compensate you have to explicitly match newline in your textscan() format at the point you expect newline to be.
  2 件のコメント
Ken
Ken 2012 年 7 月 24 日
編集済み: Ken 2012 年 7 月 24 日
Walter. Actually I got the idea from
The code there (textscan) could read a block of data into a matrix. Textscan stopped reading data-line when there was an empty line. I really don't know the trick, or maybe they used other commands? I am trying to do the same thing but it failed :(. Textscan reads everything.
I will try your advice for removing newlines from 'empty line', but sounds not an effective way ...
Walter Roberson
Walter Roberson 2012 年 7 月 24 日
That particular demo is reading blocks of floating point numbers rather than strings. Different stopping conditions internally, unfortunately.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by