Missing something obvious? (fscanf)

조회 수: 12 (최근 30일)
Ian Barker
Ian Barker 2013년 3월 21일
Trying to use fscanf to read data from a file that is 24 rows by 5 columns. I've set up this:
[fileId,errorMsg] = fopen('Data.txt', 'r');
if fileId < 0
disp(errorMsg);
else
NUMROWS = 24;
for row = 1:NUMROWS
for col = 1:5
data(row,col) = fscanf(fileId, '%d', inf);
end
end
end
but I keep getting a "Subscripted assignment dimension mismatch." error when I run it. Not sure if I'm missing some obvious error or if I'm just doing this wrong to begin with.
Thanks very much for your help in advance.
  댓글 수: 4
Image Analyst
Image Analyst 2013년 3월 21일
Is the file text, or binary?
Ian Barker
Ian Barker 2013년 3월 21일
Just a standard text file.

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

채택된 답변

Cedric
Cedric 2013년 3월 21일
편집: Cedric 2013년 3월 21일
If you need to stick to FSCANF, you can do
fid = fopen('Data.txt', 'r') ;
data = fscanf(fid, '%f', [24, 5])
fclose(fid) ;
  댓글 수: 4
Ian Barker
Ian Barker 2013년 3월 21일
편집: Ian Barker 2013년 3월 21일
What I meant to say was that I get the correct array with the correct size and numbers but in a very wrong order. The data is supposed to be organized with each category of information being in one column, but after using fscanf it seems to read and order it in some weird way.
For example I want to get this as the top row:
1 0.032 +170 1.5 -16.0
but instead i get this:
1 13.4000 9.5000 500 1.7000
Ian Barker
Ian Barker 2013년 3월 21일
Aha I think i got it. Just had to change the %d in my earlier code to a %f and it magically works now.
Thanks alot for your help anyways.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 3월 21일
You can only assign one value to data(row,col) so why do you have inf instead of 1? Anyway, why don't you just use fread() to read the whole stream of data into a 2D array directly? Why use fscanf()?
  댓글 수: 1
Ian Barker
Ian Barker 2013년 3월 21일
I'd love to use the many other easier ways of reading this file. But unfortunately I am being forced to use fscanf for some reason.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by