sprintf
Format data into string
Syntax
str = sprintf(format, A, ...)
[str, errmsg] = sprintf(format, A, ...)
Description
str = sprintf(format, A, ...) applies the format to all elements of array A and any additional array arguments in column order, and returns the results to string str.
[str, errmsg] = sprintf(format, A, ...) returns an error message string when the operation is unsuccessful. Otherwise, errmsg is empty.
Input Arguments
Examples
Example 1
Format floating-point numbers:
sprintf('%0.5f',1/eps) % 4503599627370496.00000
sprintf('%0.5g',1/eps) % 4.5036e+15
Example 2
Explicitly convert double-precision values to integers:
sprintf('%d',round(pi)) % 3
Example 3
Combine literal text with array values:
sprintf('The array is %dx%d.',2,3) % The array is 2x3
Example 4
On a Windows system, convert PC-style exponential notation (three digits in the exponent) to UNIX style notation (two digits):
a = sprintf('%e', 12345.678);
if ispc
a = strrep(a, 'e+0', 'e+');
end
Example 5
This example demonstrates the use of the width field of a format specifier to set the minimum width of the result. This is different from the sscanf function where this field represents the maximum width of the result.
The first statement uses a format of %6d, which is equal to the number of digits in the input value. sprintf returns a single value as the result:
N = sprintf('%6d', [123456])
N =
123456
The second statement passes a format specifier of %025d, in which the width field of 25 determines the minimum width of the return value:
N = sprintf('%025d', [123456])
N =
0000000000000000000123456
The 0 in the %025d format specifier indicates that you want leading zeros to be shown in the output.
Input Arguments
fileID
One ofthe following:
An integer file identifier obtained from fopen.
1 for standard output (the screen).
2 for standard error.
Default:1 (the screen)
format
String in single quotation marks that describes the format ofthe output fields. Can include combinations of the following:
Percent sign followed by a conversion character, suchas '%s' for strings.
Operators that describe field width, precision, andother options.
Literal text to print.
Escape characters, including:
''
Single quotation mark
%%
Percent character
\\
Backslash
\a
Alarm
\b
Backspace
\f
Form feed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\xN
Hexadecimal number, N
\N
Octal number, N
Conversion characters and optional operators appear in the followingorder (includes spaces for clarity):
The following table lists the available conversion charactersand subtypes.
Value Type
Conversion
Details
Integer, signed
%d or %i
Base 10
Integer, unsigned
%u
Base 10
%o
Base 8 (octal)
%x
Base 16 (hexadecimal), lowercase letters a–f
%X
Same as %x, uppercase letters A–F
Floating-point number
%f
Fixed-point notation
%e
Exponential notation, such as 3.141593e+00
%E
Same as %e, but uppercase, such as 3.141593E+00
%g
The more compact of %e or %f,with no trailing zeros
%G
The more compact of %E or %f,with no trailing zeros
%bx or %bX
%bo
%bu
Double-precision hexadecimal, octal, or decimal value
Example: %bx prints pi as 400921fb54442d18
%tx or %tX
%to
%tu
Single-precision hexadecimal, octal, or decimal value
Example: %tx prints pi as 40490fdb
Characters
%c
Single character
%s
String of characters