fscanf to fgetl pointer handoff issue?

3 views (last 30 days)
Ben Myers
Ben Myers on 28 Feb 2014
Answered: Image Analyst on 28 Feb 2014
Dear MATLAB community,
I read in the header block of a large ASCII data file using fscanf, as shown below, extracting a couple of parameters and ignoring most of the text:
fid = fopen('myfile.txt');
header1 = ...
['##File Type: %d\n' ...
'##File Format: %d\n' ...
... (more lines, lots of text and %*s) ...
##End of Header\n'];
type_and_format = fscanf(fid, header1 ,[2, 1]);
So the pointer should be sitting at the beginning of the next line after the header.
Then, I initiated a while loop using fgetl to start reading in columns of numerical data from the file:
while i < somenumber
str = fgetl(fid);
(more loop commands to process the str value and feed it into structures)
end
However, instead of the loop loading the next line into str as a string, I just get a positive integer 1 in str. The 1 appears whether "1" starts the next line or not.
Can an fgetl follow an fscanf and hand off the pointer location correctly?
Thanks in advance for your help.
Ben

Answers (1)

Image Analyst
Image Analyst on 28 Feb 2014
Might have something to do with the line terminator: \n or \n\r or whatever. You can try fgets() instead of fgetl() and see if that helps, or just skip past the bad line and don't worry about it.

Community Treasure Hunt

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

Start Hunting!