matlab中怎麽让其输出的文本数据以一定的格式输出

就是可以象FORTRAN,C语言一样的格式化输出!
2025-03-01 17:13:40
推荐回答(1个)
回答1:

FPRINTF Write formatted data to file.
COUNT = FPRINTF(FID,FORMAT,A,...) formats the data in the real
part of matrix A (and in any additional matrix arguments), under
control of the specified FORMAT string, and writes it to the file
associated with file identifier FID. COUNT is the number of bytes
successfully written. FID is an integer file identifier obtained
from FOPEN. It can also be 1 for standard output (the screen) or 2
for standard error. If FID is omitted, output goes to the screen.

FORMAT is a string containing C language conversion specifications.
Conversion specifications involve the character %, optional flags,
optional width and precision fields, optional subtype specifier, and
conversion characters d, i, o, u, x, X, f, e, E, g, G, c, and s.
See the Language Reference Guide or a C manual for complete details.

The special formats \n,\r,\t,\b,\f can be used to produce linefeed,
carriage return, tab, backspace, and formfeed characters respectively.
Use \\ to produce a backslash character and %% to produce the percent
character.

FPRINTF behaves like ANSI C with certain exceptions and extensions.
These include:
1. Only the real part of each parameter is processed.
2. The following non-standard subtype specifiers are supported for
conversion characters o, u, x, and X.
t - The underlying C datatype is a float rather than an
unsigned integer.
b - The underlying C datatype is a double rather than an
unsigned integer.
For example, to print out in hex a double value use a format like
'%bx'.
3. FPRINTF is "vectorized" for the case when A is nonscalar. The
format string is recycled through the elements of A (columnwise)
until all the elements are used up. It is then recycled in a similar
manner through any additional matrix arguments.

For example, the statements

x = 0:.1:1; y = [x; exp(x)];
fid = fopen('exp.txt','w');
fprintf(fid,'%6.2f %12.8f\n',y);
fclose(fid);

create a text file containing a short table of the exponential function:

0.00 1.00000000
0.10 1.10517092
...
1.00 2.71828183

See also FSCANF, SPRINTF, FWRITE, DIARY, SAVE, INPUT.