Skip specific line from a text file

4 ビュー (過去 30 日間)
MS11
MS11 2020 年 9 月 22 日
回答済み: Walter Roberson 2020 年 9 月 22 日
I want to read 2041 text files from a folder but mostly files contain 'error found' at the last. How can I skip that line from reading by the code. Picture of the file has been attached.

回答 (2 件)

Ruger28
Ruger28 2020 年 9 月 22 日
One approach using fgetl. Another would be to read the entire file in, and just search and throw out that line.
fname = '409.txt'
FID = fopen(fname,'rt');
cntr = 1;
loopEn = 1;
while loopEn
temp = fgetl(FID);
if ~strcmpi(temp,'e r r o r f o u n d') % skip line of error found
FileData{cntr} = temp;
cntr = cntr + 1;
elseif temp == -1
break;
end
end
  1 件のコメント
MS11
MS11 2020 年 9 月 22 日
thank you Ruger but there are 2041 files and mostly contain the same problem. How can SKIP this from all files so my code doesn't stop.

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


Walter Roberson
Walter Roberson 2020 年 9 月 22 日
If you use fopen / textscan / fclose, then you can specify 'commentstyle', 'e r r' . That tells it to skip from 'e r r' to the end of the line when it it is reading in data.

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by