MATLAB: Fopen, fscanf, fid

fidfopen

Hi I am debugging a set of matlab codes.
in code below if I comment out the second line I get the error that "Invalid file identifier. Use fopen to generate a valid file identifier." and I do not understand the role of second line because later on it will never be used?!!
Besides, I do not understand the two lines to the end
fid=fopen('ID.dat');
line=fgetl(fid);
rep=fscanf(fid,'%s',1);
fclose('all');
fid=fopen([rep,filesep,'Nemoh.cal'],'r');
for i=1:6
ligne=fgetl(fid);
end
nBodies=fscanf(fid,'%g',1);
fclose(fid);
fid=fopen([rep,filesep,'Nemoh.cal'],'r');
n=1;
clear textline;
textline={};

Best Answer

Using fgetl() on a file reads a line from the file. It is possible that there is no need to process the line further, such as if it is a header line or comment: in such a case you still need to read the line in order to be in position to read the data that you do want.
Note that there is no way in MS Windows, OS-X or Linux to say that you want to position to a particular line number of a file -- no way except to start a the beginning and read line by line until you reach the one you want.