keep fscanf past wrong format value
10 ビュー (過去 30 日間)
古いコメントを表示
Hello, im having this problem with feof function and fscanf. I want to read all the whole numbers but it stops when it reaches something that is not. How can I make it ignore that and keep scanning?
fid = fopen("testdata.txt","r");
a = [];
i = 0;
while ~feof(fid)
i = i + 1;
a(i) = fscanf(fid, '%d', 1);
end
fclose(fid);
1 件のコメント
Rik
2023 年 5 月 15 日
Wouldn't you be better off reading the entire file and then matching each line with a regular expression?
採用された回答
Walter Roberson
2023 年 5 月 15 日
Unless you have integers that might have value outside of +/- 2^53 then there is little point using a %d format. Use a %f format instead: that will accept floating point numbers without any reading problems. You can always check that everything is integer afterwards.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Low-Level File I/O についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!