keep fscanf past wrong format value

조회 수: 10 (최근 30일)
Santiago
Santiago 2023년 5월 15일
답변: Walter Roberson 2023년 5월 15일
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
Unable to perform assignment because the left and right sides have a different number of elements.
fclose(fid);
  댓글 수: 1
Rik
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
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개)

카테고리

Help CenterFile 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!

Translated by