fscanf not giving any output

31 views (last 30 days)
Jacob Allen
Jacob Allen on 4 Sep 2022
Commented: dpb on 4 Sep 2022
Attached below is the current code I have
fid = fopen('sample_velmod.txt','r');
formatspec = '%d %d\n';
sizeA = [2 inf];
A = fscanf(fid,formatspec,sizeA);
fclose(fid);
However, when I run this, I get no output for A. The text file has been implemented into the matlab directory so I know that it is reading the file fine. Any help getting and output for A would be great.

Accepted Answer

dpb
dpb on 4 Sep 2022
Edited: dpb on 4 Sep 2022
Absolutely no way to debug without the data file, but either
  1. The file is empty even if it exists, or
  2. The file content is such that the format spec fails before converting any data
Both of those boil down to the same thing; the format spec and the file aren't in synch but there's absolutely no way to have any klew how to fix without being able to see the file content itself...and NOT just pasted-in text from the file, but the file itself.
The far simpler/better way to do this with any recent release of MATLAB, certainly with R2021a would be
A=readmatrix('sample_velmod.txt');
ADDENDUM
Looking at the file content will almost certainly explain the problem encountered, but you can do some debugging and see what went on a little if use
[A,n] = fscanf(fid,formatspec,sizeA);
n will return the count of elements read; probably going to be zero. You can also then look at
ftell(fid)
after the call and see how many bytes into the file the file pointer got before it barfed...those are "deep diving" and more for interest than anything, but will show you what happened internally. "Why" will undoubtedly be explained by looking at the file itself far more easily.
From the documentation, there's the following that 'splains expected behavior and so result isn't all that surprising --
"...If fscanf cannot match formatSpec to the data, it reads only the portion that matches and stops processing."
That's what happened to you, the format spec obviously didn't match the data and vice versa.
  5 Comments
Jacob Allen
Jacob Allen on 4 Sep 2022
Thanks for the feedback, it definitely helped a ton. Unfortunately this is part of an assignment so I have to use the slow and non-innovative way.
dpb
dpb on 4 Sep 2022
Glad to help -- shows that one has to look at the input carefully -- in this case would have been able to see the problem with just the text and not necessarily the file; can't always tell that so is always safest to supply the real thing first.
Couple observations re: the sidebar conversation on what goes on internally ...
>> fid=fopen('simple_velmod.txt');
>> [A,N]=fscanf(fid,'%d %d\n',[2 inf]); % your original case -- see what returns
>> N
N =
1
>> A
A =
0
>> whos A
Name Size Bytes Class Attributes
A 1x1 8 double
>> ftell(fid)
ans =
1
>> fscanf(fid,'%c',1)
ans =
'.'
>>
The above sequence shows that the '%d' DID match the first 0 and successfully converted it under the decimal format string. It then barfed on the decimal point scanning for another value; the file pointer was pointing to that next character that ftell shows us (file position starts with/is zero-based counting) and reading the next character with '%c' in fact returned it.
Carrying on from that diagnostics...
>> frewind(fid) % go back to beginning, no need close/reopen file...
>> [A,N]=fscanf(fid,'%f %f\n',[2 inf]);
>> whos A
Name Size Bytes Class Attributes
A 2x25 400 double
>>
NOTA BENE: the array is only two rows but 25 columns -- not what reflects the orientation of the input file. That's the result of MATLAB internal storage order being column-first; the two values of the first row in the file end up in the first column of the output array when you give it the size argument.
So, a general rule in low-level i/o is use
A=fscanf(fid,'%f %f\n',[2 inf]).'; % transpose the 2xN array back to Nx2 to match file

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Sep 2022
fscanf() will return empty under either of two conditions:
  • the file is empty; OR
  • the data at the file position does not match the first element of the format specification.
For example, the format that you used would fail if there is a header line on the file, unless you read the header line first.
I would suggest that you use readmatrix() as readmatrix() will automatically detect and discard a header line if present.
  2 Comments
Walter Roberson
Walter Roberson on 4 Sep 2022
Your file contains, for example,
1100.00 3366.58
Those are not integers.
Also, your file has tab characters in it, and carriage returns. You need
formatspec = '%f\t%f';
Jacob Allen
Jacob Allen on 4 Sep 2022
Thank you for the feedback!

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!