MATLAB Function Reference |
Write formatted data to string
Syntax
Description
[s, errmsg] = sprintf(format, A, ...)
formats the data in matrix A
(and in any additional matrix arguments) under control of the specified format
string and returns it in the MATLAB string variable s
. The sprintf
function returns an error message string errmsg
if an error occurred. errmsg
is an empty matrix if no error occurred.
sprintf
is the same as fprintf
except that it returns the data in a MATLAB string variable rather than writing it to a file.
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 nonprinting characters such as newline characters and tabs.
Conversion specifications begin with the %
character and contain these optional and required elements:
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.
The following tables describe the nonalphanumeric characters found in format specification strings.
This table lists the escape character sequences you use to specify non-printing characters in a format specification.
Character |
Description |
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Horizontal tab |
\\ |
Backslash |
Single quotation mark |
|
%% |
Percent character |
Remarks
The sprintf
function behaves like its ANSI C language namesake with these exceptions and extensions.
sprintf
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 the fix
, floor
, ceil
, or round
functions to change the value in the double into a value that can be represented as an integer before passing it to sprintf
.
%o
, %u
, %x
, and %X
.sprintf
function is vectorized for nonscalar arguments. The function recycles the format string through the elements of A
(columnwise) until all the elements are used up. The function then continues in a similar manner through any additional matrix arguments.
%s
is used to print part of a nonscalar double argument, the following behavior occurs:
%s
specifier and is used for a later specifier. For example,
pi
terminates the string below and is printed using %f
format.
%s
specifier using an e
conversion as a warning to the
user. For example, pi
is formatted by %s
below in exponential notation,
and 65
, though representing a valid character, is formatted as fixed-point
(%f
).
%s
prints nothing and the value is skipped. If zero is found after at least one
valid character, it terminates the printing for this %s
specifier and is used
for a later specifier.
sprintf
prints negative zero and exponents differently on some platforms, as shown in the following tables. Display of Negative Zero | |||
Platform |
%e or %E |
%f |
%g or %G |
PC |
0.000000e+000 |
0.000000 |
0 |
Others |
-0.000000e+00 |
-0.000000 |
-0 |
Platform |
Minimum Digits in Exponent |
Example |
PC |
3 |
1.23e+004 |
UNIX |
2 |
1.23e+04 |
You can resolve this difference in exponents by postprocessing the results of sprintf
. For example, to make the PC output look like that of UNIX, use
Examples
See Also
References
[1] Kernighan, B.W., and D.M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988.
[2] ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.
sprank | spy |
© 1994-2005 The MathWorks, Inc.