-1

I have a problem with this easy code

name=['r9460.txt']
fid=fopen(name,'r');
while ~(feof(fid)),
    head=fscanf(fid,'%d',11)
    if ~(isempty(head)),
        m=head(4)
        d=head(3)
        y=head(2)
    end
end
fclose(fid)

The txt is

5  1994     4   8       9  14  40       0  31  16        5
        0.6            1
        0.6           25
        0.9            1
        3.5            4
        0.9            1

Matlab says

Attempted to access head(4); index out of bounds because numel(head)=1.

Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • 1
    Could `head` be a `cell` and not a matrix? In that case you would need to use `m=head{4}`. What on-screen output do you get for `head`? – Schorsch Feb 20 '14 at 10:30
  • is it possible you get the error at the second iteration? what is the value of `head`? Have you tried using a debugger???? – Shai Feb 20 '14 at 11:03
  • head is loaded as 11x1, and if i use m=head{4} it gives me error;(how can i modify it to a cell?I'm a beginner). i used debugger and i saw that it cycles two times, it shouldn't do it because it ended the file. Have you got a solution? If i add another line i see that head doesn't take new values but 0. – ilpigna Feb 20 '14 at 11:30

1 Answers1

0

In the first iteration of the while loop, it reads those first 11 values from the header line, and assigns values m, d and y from those values correctly.

However, you haven't reached the end of the file yet (feof(fid) returns 0). So the while loop continues; it reads the next line. Looking at your code, I don't think you intend to read in the header line more than once - if your loop went any more than one time you would be overwriting the m/d/y values. So why the loop at all? If you're trying to read the header then do something with the rest of the file, you might be looking for:

name=['r9460.txt']
fid=fopen(name,'r');

head=fscanf(fid,'%d',11)
    if ~(isempty(head)),
        m=head(4)
        d=head(3)
        y=head(2)
    end

while ~(feof(fid)),
   % do something with the remaining data in the file
end

fclose(fid)

One last note - if the rest of your data after the header looks like the above, two numerical values, and you don't need to perform any line by line checking on it, you can read it in in a single shot with data = textscan(fid,'%f%f'); and not have any loop at all.

nkjt
  • 7,825
  • 9
  • 22
  • 28
  • Thanks for your answer; the code is longer than this one, and i have to do other thinks with the value m,d,y; here i posted the short version to facilitate you. The txt is longer than this one, it is based on other 500 lines of header line. So your answer isn't very useful for my problem, because i need to read header, save m,d,y do something with these value, than go on with the txt read another header line and do something new with the new m,d,y... Anyway i tried your solution but the problem is that it never stop the while feof loop! Thanks so much for your help! – ilpigna Feb 21 '14 at 10:25
  • That's because that's not intended to be fully working code, just pseudocode - nothing is inside the `while` loop. The main point remains - your `while` loop is trying to read in 11 `%d` values at a time, and assign the fourth value to `m`. If you hit a line which doesn't have 4 values (as in your sample data), it will fail. If you have header line + data + header line + data 500 times over, you need to add something in your while loop that loads in the data part. – nkjt Feb 21 '14 at 10:48