fgetl skips lines beginning with #

2 views (last 30 days)
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
  5 Comments
John J. Jasbinsek
John J. Jasbinsek on 1 Jan 2019
Wow - now I tried it with the .csv extension again, and it works for me now like it did for you.
I did restart Maltab in the meantime but that's really odd.
Anyway - it all seems to work now. Thank you for your time and help.
Walter Roberson
Walter Roberson on 1 Jan 2019
I woner if perhaps the actual file was UTF encoded or something like that?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 1 Jan 2019
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);
  6 Comments
Image Analyst
Image Analyst on 2 Jan 2019
It could be something like you didn't clear all your variables with
clear all
and when you were using the fileID you were somehow using the one from when it was the other file. Maybe you changed the name of only some fid variables in your program but not all of them or something like that.
John J. Jasbinsek
John J. Jasbinsek on 3 Jan 2019
That couldb be it. I generally do a good job of closing "fid" but maybe when I was doing my quick tests I didn't do it. However in spending a little time trying to replicate the problem doing that didn't cause a problem this time. I think I'll never know. Anyway, it seems to always work as expected now.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!