0

i am trying to read data from a text file. The data are just lines with random words. im trying to skip the empty lines in this file and not load it. I use a loop with fgetl to load the data and the statement feof(fid)==0 to know when the end of the file is reached. I try use if isempty to skipe those lines. But the problem is that it also skips the line after the empty one. I think it is a problem within the loop. Is there another way to express feof(fid) so that it dosent conflict?

while feof(fid)==0 
    randomline=fgetl(fid) 
    if isempty(aline) 
        randomline=fgetl(fid) 
    else 
        %store data 
    end 
end
sco1
  • 12,154
  • 5
  • 26
  • 48

1 Answers1

4

fgetl increments the line counter. Adding another call to fgetl in your data checking logic is why it's incrementing a second time.

while ~feof(fid) 
    randomline = fgetl(fid) 
    if ~isempty(randomline) 
        % if line is not empty, store the line
    end
end
sco1
  • 12,154
  • 5
  • 26
  • 48