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
4

1 回答 1

4

fgetl增加行计数器。在数据检查逻辑中添加另一个调用fgetl是它第二次递增的原因。

while ~feof(fid) 
    randomline = fgetl(fid) 
    if ~isempty(randomline) 
        % if line is not empty, store the line
    end
end
于 2014-05-13T16:17:56.400 回答