| MATLAB Function Reference | Search  Help Desk |
| sprintf | Examples See Also |
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.t |
The underlying C data type is a float rather than an unsigned integer. |
b |
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
Character
Description
\n New line
\t Horizontal tab
\b Backspace
\r Carriage return
\f Form feed
\\ Backslash
\'' or ''
(two single quotes)
Single quotation mark
%% Percent character
Conversion Specifiers
% and the conversion character.
Other Characters
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.