MATLAB Function Reference Previous page   Next Page
sprintf

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.

Character
Description
Example
A minus sign (-)
Left-justifies the converted argument in its field
%-5.2d
A plus sign (+)
Always prints a sign character (+ or -)
%+5.2d
Zero (0)
Pad with zeros rather than spaces.
%05.2d

Field Width and Precision Specifications

You can control the width and precision of the output by including these options in the format string.

Character
Description
Example
Field width
A digit string specifying the minimum number of digits to be printed.
%6f
Precision
A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point
%6.2f

Conversion Characters

Conversion characters specify the notation of the output.

Specifier
Description
%c
Single character
%d
Decimal notation (signed)
%e
Exponential notation (using a lowercase e as in 3.1415e+00)
%E
Exponential notation (using an uppercase E as in 3.1415E+00)
%f
Fixed-point notation
%g
The more compact of %e or %f, as defined in [2]. Insignificant zeros do not print.
%G
Same as %g, but using an uppercase E
%o
Octal notation (unsigned)
%s
String of characters
%u
Decimal notation (unsigned)
%x
Hexadecimal notation (using lowercase letters a-f)
%X
Hexadecimal notation (using uppercase letters A-F)

The following tables describe the nonalphanumeric characters found in format specification strings.

Escape Characters

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

\'' or ''

(two single quotes)

Single quotation mark
%%
Percent character

Remarks

The sprintf function behaves like its ANSI C language namesake with these exceptions and extensions.

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

Command
Result
sprintf('%0.5g',(1+sqrt(5))/2)
1.618
sprintf('%0.5g',1/eps)
4.5036e+15
sprintf('%15.5f',1/eps)
4503599627370496.00000
sprintf('%d',round(pi))
3
sprintf('%s','hello')
hello
sprintf('The array is %dx%d.',2,3)
The array is 2x3
sprintf('\n')
Line termination character on all platforms

See Also

int2str, num2str, sscanf

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.


Previous page  sprank spy Next page

© 1994-2005 The MathWorks, Inc.