Missing something obvious? (fscanf)

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

Could you copy/paste this file here as a comment? The FSCANF won't work a priori, and I guess that we should discuss the approach.
If you do the following, you should see that it cannot work
>> [fileId,errorMsg] = fopen('Data.txt', 'r') ;
>> fscanf(fileId, '%d', inf)
ans =
.. whatever you'll get
>> fscanf(fileId, '%d', inf)
ans =
.. and here it should start not to return what you want ..
>> fscanf(fileId, '%d', inf)
ans =
.. and again ..
>> fscanf(fileId, '%d', inf)
ans =
.. and again ..
...
Not sure exactly what you mean by this. I have an example script which is quite similar to the code I posted and it runs fine, but for some reason when I tried the same thing with this file it did not work. BTW the file looks like this
1 0.032 +170 1.5 -16.0
2 0.034 +290 0.5 17.2
6822 0.214 -130 9.0 12.7
598 0.263 -70 7.0 15.1
221 0.275 -185 8.8 13.4
224 0.275 -220 5.0 17.2
5457 0.45 +200 9.9 13.3
4736 0.5 +290 8.4 15.1
5194 0.5 +270 7.4 16.1
4449 0.63 +200 9.5 14.5
4214 0.8 +300 11.3 13.2
3031 0.9 -30 8.3 16.4
3627 0.9 +650 9.1 15.7
4826 0.9 +150 9.0 15.7
5236 0.9 +500 10.4 14.4
1068 1.0 +920 9.1 15.9
5055 1.1 +450 9.6 15.6
7331 1.1 +500 10.4 14.8
4258 1.4 +500 8.7 17.0
4151 1.7 +960 12.0 14.2
4382 2.0 +500 10.0 16.5
4472 2.0 +850 8.8 17.7
4486 2.0 +800 9.7 16.8
4649 2.0 +1090 9.5 17.0
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일

0 개 추천

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일
That works, but the problem is the data is then so jumbled up I can't index it later. Unless there is some way to reorganize it instead of having the output as line by line?
Thanks for helping.
On my system I get a 24x5 numeric array, as specified by the 3rd argument of FSCANF. Did you keep Inf as a 3rd arg? If not, which version of MATLAB are you using?
If you get a 120x1 array, you can reshape it the following way
data = reshape(data, 5, []).' ;
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일

0 개 추천

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.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2013년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by