How come fscanf is only reading the first row of the given txt file?

16 views (last 30 days)
I'm trying to extract the data in the given text file. The comments are in the file just for reference and need to be ignored when inputting into matlab. I attempted to do this through the fscanf function but it would only extract the first row. I was only being returned [4;3] which is the format I want it to be in but I would like it to do this for every row. I also attempted this by using a for loop a fgetl but the string comments make this difficult to put into the needed vector form.
  5 Comments
Aggie1313
Aggie1313 on 20 Oct 2016
Is there a way that fscanf can be used for specific rows so that I can extract what I want in a for loop?
Walter Roberson
Walter Roberson on 20 Oct 2016
No. fscanf() always moves forward from the current position. It stops reading as soon as the format does not match; it also stops reading as soon as any given count is complete, even if there are more items in the format.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Oct 2016
A = fscanf(fid,'%f%f%*[^\n]', [5,2]);
A2 = fscanf(fid,'%f%f%f%*[^\n]',[4 3]);
junk = fgetl(fid);
A3 = fscanf(fid,'%f',[1 1]);
junk = fgetl(fid);
A4 = fscanf(fid,'%f',[1 A3]);
junk = fgetl(fid);
A5 = fscanf(fid,'%f',[1 1]);
junk = fgetl(fid);
... that sort of thing

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!