fscanf: how does the formatting string work?
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!