필터 지우기
필터 지우기

Issue with fscanf not reading more than 1 row

조회 수: 10 (최근 30일)
D
D 2011년 10월 22일
I'm trying to pull data from a file using fscanf. The data file is formatted as you see in the fscanf below, and has 20 rows. I'm only getting 1 row's worth of data stored into A. I thought fscanf was supposed to read till the end of the file but it seems to only be reading the one line. Is there a way to correct it so it stores the data from all 20 rows?
fid = fopen('myfile.dat');
fgetl(fid); fgetl(fid);
A = fscanf(fid,'%f %f %f %f %*s %*s');
B = reshape(A,4,20)'

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 22일
Check whether you have any special characters at the end of each line. I have similar program. It didn't have problem. Add fclose(fid) at the end just in case.
myfile.data contains the following
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
1 2 3 4 a b
Run your code, it has
B =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
  댓글 수: 1
D
D 2011년 10월 22일
Thanks, I actually found the problem after you said it worked. I added another %*s at the end of the formatting. I put one for the column of names, neglecting to consider first and last names, so it needed an extra to complete the line.

댓글을 달려면 로그인하십시오.

추가 답변 (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