MATLAB: Reading header line from text file

fgetsheadertext filetextscan

Hi,
I have text file with 2 header lines and data (7 columns). I can read the data using textscan, but not able to read header file (using fgets). Please see the code below:
fid = fopen(files(1).name, 'rt');
C = textscan(fid, '%f %f %f %f %f %f %f' ,'HeaderLines',2,'Delimiter',',');
tline = fgets(fid);
fclose(fid);
It gave tline = -1.
Any help regarding the problem, will be really appreciated. Thanks, Rami

Best Answer

It means the position indicator is not at the beginning of the file after it exits textscan:
You can either rewind:
fid = fopen(files(1).name, 'rt');
C = textscan(fid, '%f %f %f %f %f %f %f' ,'HeaderLines',2,'Delimiter',',');
frewind(fid);
tline = fgets(fid);
fclose(fid);
Or read it first:
fid = fopen(files(1).name, 'rt');
line1 = fgets(fid);
line2 = fgets(fid);
C = textscan(fid, '%f %f %f %f %f %f %f' ,'Delimiter',',');
tline = fgets(fid);
fclose(fid);
Related Question