Generic code for Reading from Files:
The generic code to complete this is as shown below:
fid = fopen('filename');
if fid == -1
disp('File open not successful')
else
while feof(fid) == 0
% Read one line into a string variable
aline = fgetl(fid);
% Use string functions to extract numbers, strings,
% etc. from the line
% Do something with the data!
end
closeresult = fclose(fid);
if closeresult == 0
disp('File close successful')
else
disp('File close not successful')
end
end
The permission string can be involved in the call to the fopen function, for illustration,
fid = fopen('filename', 'r');
but is not essential as reading is the default. The condition on the while loop can be interpreted by saying "while the file end-of-file is false." The other way to write this is
while feof(fid)
that is interpreted as "while we're not at the end of the file."