fscanf: how does the formatting string work?
Show older comments
So looking at the example from Mathworks documentation:
% Create a file with an exponential table
x = 0:.1:1;
y = [x; exp(x)];
*Non-pertinent code omitted***
fid = fopen('exp.txt');
A = fscanf(fid, '%g %g', [2 inf]);
fclose(fid);
% Transpose so that A matches
% the orientation of the file
A = A';
Outputs a 11x2 matrix of numbers. Now, if I change the format field to:
- '%g': Outputs the same 2-column matrix of numbers (still 11 rows)
- '%f %g': Outputs the same 2-column matrix of numbers (still 11 rows)
- '%s %g': Outputs a 28x2 matrix. Some of the entries (every 4th one to be exact) are the same as the original matrix. Everything else seems to be semi-random numbers, all between 46 and 57.
- '%s': Outputs a 77x2 matrix. All random numbers.
What's going on here? I think there is something fundamental I'm not understanding about the fscanf.
Non-pertinent lines ommitted. Here's the link for the full example: http://www.mathworks.com/help/matlab/ref/fscanf.html
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings 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!