|MATLAB Function Reference|
www.kxcad.net Home > CAE Software Index > MATLAB Index >
Write formatted data to string
[s, errmsg] = sprintf(format, A, ...)
[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.
See Formatting Strings in the MATLAB Programming documentation for more detailed information on using string formatting commands.
The format argument is a string containing ordinary characters and/or 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:
Width and precision fields (optional)
A subtype specifier (optional)
Conversion character (required)
You specify these elements in the following order:
You can control the alignment of the output using any of these optional flags.
A minus sign (-)
Left-justifies the converted argument in its field
A plus sign (+)
Always prints a sign character (+ or –)
Pad with zeros rather than spaces.
You can control the width and precision of the output by including these options in the format string.
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
Conversion characters specify the notation of the output.
Decimal notation (signed)
Exponential notation (using a lowercase e as in 3.1415e+00)
Exponential notation (using an uppercase E as in 3.1415E+00)
The more compact of %e or %f, as defined in . Insignificant zeros do not print.
Same as %g, but using an uppercase E
Octal notation (unsigned)
String of characters
Decimal notation (unsigned)
Hexadecimal notation (using lowercase letters a–f)
Hexadecimal notation (using uppercase letters A–F)
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.
(two single quotes)
Single quotation mark
The sprintf function behaves like its ANSI C language namesake with these exceptions and extensions.
If you use 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.
The following nonstandard subtype specifiers are supported for the conversion characters %o, %u, %x, and %X.
The underlying C data type is a double rather than an unsigned integer. For example, to print a double-precision value in hexadecimal, use a format like '%bx'.
The underlying C data type is a float rather than an unsigned integer.
For example, to print a double value in hexadecimal use the format '%bx'.
The 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.
If %s is used to print part of a nonscalar double argument, the following behavior occurs:
Successive values are printed as long as they are integers and in the range of a valid character. The first invalid character terminates the printing for this %s specifier and is used for a later specifier. For example, pi terminates the string below and is printed using %f format.
Str = [65 66 67 pi]; sprintf('%s %f', Str) ans = ABC 3.141593
If the first value to print is not a valid character, then just that value is printed for this %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).
Str = [pi 65 66 67]; sprintf('%s %f %s', Str) ans = 3.141593e+000 65.000000 BC
One exception is zero, which is a valid character. If zero is found first, %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.
Negative Zero Printed with %e, %E, %f, %g, or %G
Display of Negative Zero
%e or %E
%g or %G
Exponents Printed with %e, %E, %g, or %G
Minimum Digits in Exponent
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
a = sprintf('%e', 12345.678); if ispc, a = strrep(a, 'e+0', 'e+'); end
|sprintf('The array is %dx%d.',2,3)||The array is 2x3|
Line termination character on all platforms
int2str, num2str, sscanf
 Kernighan, B.W., and D.M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988.
 ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018.