Syntax
Description
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
. fprintf
returns a count of the number of bytes written.
Argument fid
is an integer file identifier obtained from fopen
. (It may also be 1
for standard output (the screen) or 2
for standard error. See fopen
for more information.) Omitting fid
causes output to appear on the screen.
Format String
The format
argument is a string containing C language conversion specifications. A conversion specification controls the notation, alignment, significant digits, field width, and other aspects of output format. The format string can contain escape characters to represent non-printing characters such as newline characters and tabs.
Conversion specifications begin with the %
character and contain these optional and required elements:
- Flags (optional)
- Width and precision fields (optional)
- A subtype specifier (optional)
- Conversion character (required)
You specify these elements in the following order:
Flags
You can control the alignment of the output using any of these optional flags.
Field Width and Precision Specifications
You can control the width and precision of the output by including these options in the format string.
Conversion Characters
Conversion characters specify the notation of the output.
Conversion characters %o
, %u
, %x
, and %X
support subtype specifiers. See Remarks for more information.
This table lists the escape character sequences you use to specify non-printing characters in a format specification.
Character | Description |
| Backspace |
| Form feed |
| New line |
| Carriage return |
| Horizontal tab |
| Backslash |
Single quotation mark | |
%% | Percent character |
Remarks
The fprintf
function behaves like its ANSI C language namesake with these exceptions and extensions.
- If you use
fprintf
to convert a MATLAB double into an integer, and the double contains a value that cannot be represented as an integer (for example, it contains a fraction), MATLAB ignores the specified conversion and outputs the value in exponential format. To successfully perform this conversion, use thefix
,floor
,ceil
, orround
functions to change the value in the double into a value that can be represented as an integer before passing it tosprintf
. - The following, non-standard subtype specifiers are supported for the conversion characters
%o
,%u
,%x
, and%X
.
- The
fprintf
function is vectorized for nonscalar arguments. The function recycles the format string through the elements ofA
(columnwise) until all the elements are used up. The function then continues in a similar manner through any additional matrix arguments.
Note fprintf displays negative zero (-0) differently on some platforms, as shown in the following table. |
Conversion Character | |||
Platform | %e or %E | %f | %g or %G |
PC | 0.000000e+000 | 0.000000 | 0 |
SGI | 0.000000e+00 | 0.000000 | 0 |
HP700 | -0.000000e+00 | -0.000000 | 0 |
Others | -0.000000e+00 | -0.000000 | -0 |
Examples
create a text file called exp.txt
containing a short table of the exponential function:
displays a line on the screen:
To insert a single quotation mark in a string, use two single quotation marks together. For example,
Explicitly convert MATLAB double-precision variables to integral values for use with an integral conversion specifier. For instance, to convert signed 32-bit data to hexadecimal format:
No comments:
Post a Comment