Because in the first case, the fgetl() function returns an empty vector char vector, and comparison of an empty vector with -1 produce an empty logical vector which MATLAB interpret as false. To avoid this in the first case before the end of while loop add
if isempty(line)
line = ' ';
end
this will prevent the while condition to fail.
Best Answer