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.