Command fscanf does not read zero
이전 댓글 표시
Hello,
I am using a command "fscanf", but it doesn't read zero at the end of the line.
A file which I am reading contains this formatting:
1 552.45 1 1 1 0
2 870.38 1 2 2 0
name='test.txt';
fid1=fopen(name,'rt');
ab=fscanf(fid1,'%4f %5f %4f %4f %4f %4f',[1,6])
Can you help me to correct it?
답변 (2개)
This looks correct to me:
fid = fopen('test.txt','rt');
mat = fscanf(fid,'%f',[6,Inf]).'
fclose(fid);
For comprison:
type test.txt
Walter Roberson
2026년 6월 30일 17:50
이동: Walter Roberson
2026년 6월 30일 17:51
Your width field is not wide enough for the second field, so the trailing '5' is read as being the next field.
name = 'test.txt';
fid1 = fopen(name,'rt');
ab = fscanf(fid1,'%4f %5f %4f %4f %4f %4f',[1,6])
frewind(fid1)
ab = fscanf(fid1,'%4f %6f %4f %4f %4f %4f',[1,6])
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!