MATLAB Functions | Help Desk |
sprintf
Write formatted data to a string
s = sprintf(format
,A,...) [s,errrmsg] = sprintf(format
,A,...)
s = 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
. sprintf
is the same as fprintf
except that it returns the data in a MATLAB string variable rather than writing it to a file.
The format
string specifies notation, alignment, significant digits, field width, and other aspects of output format. It can contain ordinary alphanumeric characters; along with escape characters, conversion specifiers, and other characters, organized as shown below: [s,errrmsg] = sprintf(format
,A,...)
returns an error message string errmsg
if an error occurred or an empty matrix if an error did not occur.
The sprintf
function behaves like its ANSI C language sprintf()
namesake with certain exceptions and extensions. These include:
%o
, %u
, %x
, and %X
.The underlying C data type is a float rather than an unsigned integer. |
|
The underlying C data type is a double rather than an unsigned integer. |
%bx
'.
sprintf
is vectorized for the case when input matrix A
is nonscalar. The format string is cycled through the elements of A
(columnwise) until all the elements are used up. It is then cycled in a similar manner, without reinitializing, through any additional matrix arguments.
Escape Characters
\'' or ''
Conversion Specifiers Exponential notation (using an uppercase The more compact of
E
as in
3.1415E+00
)
%e
or %f
, as defined in [2]. Insignificant
zeros do not print.
%
and the conversion character.
Other Characters A digit string specifying the minimum
number of digits to be printed.
A digit string including a period (.)
specifying the number of digits to be
printed to the right of the decimal point.
|
---|
int2str,num2str
,sscanf
[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.