MATLAB: Fgetl skips lines beginning with #

fgetlMATLAB

Hello,
I have a csv file that looks like this:
# dataset: GeoCSV 2.0
# delimiter: ,
# SID: IM_NV01__SHZ
# sample_count: 60001
# sample_rate_hz: 40
# start_time: 2007-07-23T22:30:09.000000Z
# latitude_deg: 38.429501
# longitude_deg: -118.303703
# elevation_m: 2040.0
# depth_m: 0.0
# azimuth_deg: 0.0
# dip_deg: -90.0
# instrument: GS13-NVAR=NV01=Gen=AIM24S_8.11E6=NV01
# scale_factor: 9.2265902E9
# scale_frequency_hz: 1.0
# scale_units: M/S
# field_unit: UTC, M/S
# field_type: datetime, FLOAT
Time, Sample
2007-07-23T22:30:09.000000Z, -9.3528399e-08
The remaing lines are like the last line above (a date/time and data value)
I want to read in all the lines beginning, those with the #, as well as the data lines but I find that using the fgetl command to read a line skips all the lines beginning with #
Here is what I tried as an initial test, but I was surprised by the output as it skipped all the lines beginning with #:
fid = fopen('geocsvmod.csv','r');
tline1 = fgetl(fid);
tline2 = fgetl(fid);
fclose(fid);
The output I got is:
>> tline1
tline1 =
'Time, Sample'
>> tline2
tline2 =
'2007-07-23T22:30:09.000000Z, -9.3528399e-08'
I can't find any documentation that fgetl should skip a line beginning with #
I would like to simply read each line in one by one and process them
as I go, including the lines with #
I hope I am not missing something obvious. Other commands for reading in data such as importdata appear to do this as well
Any advice?
Thank you

Best Answer

Try this:
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if strcmpi(textLine(1), '#') % You can also use startsWith(textLine, '#') in newer versions.
% Skip this line
continue;
end
% Process this line if you get here.
% Now read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);