left-justify: 12.30
'
Display a + sign for positive numbers. The default behavior is to omit the leading + sign for positive
numbers.
txt = sprintf('no sign: %12.2f\nsign: %+12.2f',...
12.3, 12.3)
txt =
'no sign:
12.30
sign:
+12.30'
Pad to the left with spaces and zeroes. The default behavior is to pad with spaces.
txt = sprintf('Pad with spaces: %12.2f\nPad with zeroes: %012.2f',...
5.2, 5.2)
txt =
'Pad with spaces:
5.20
Pad with zeroes: 000000005.20'
Note You can specify more than one flag in a formatting operator.
Value Identifiers
By default, functions such as sprintf insert values from input arguments into the output text in
sequential order. To process the input arguments in a nonsequential order, specify the order using
numeric identifiers in the format specifier. Specify nonsequential arguments with an integer
immediately following the % sign, followed by a $ sign.
Ordered Sequentially
Ordered By Identifier
sprintf('%s %s %s',...
'1st','2nd','3rd')
ans =
'1st 2nd 3rd'
sprintf('%3$s %2$s %1$s',...
'1st','2nd','3rd')
ans =
'3rd 2nd 1st'
Special Characters
Special characters can be part of the output text. But because they cannot be entered as ordinary
text, they require specific character sequences to represent them. To insert special characters into
output text, use any of the character sequences in the table.
Special Character
Representation in Format
Specifier
Single quotation mark
''
Percent character
%%
Backslash
\\
Formatting Text
6-27
'
Display a + sign for positive numbers. The default behavior is to omit the leading + sign for positive
numbers.
txt = sprintf('no sign: %12.2f\nsign: %+12.2f',...
12.3, 12.3)
txt =
'no sign:
12.30
sign:
+12.30'
Pad to the left with spaces and zeroes. The default behavior is to pad with spaces.
txt = sprintf('Pad with spaces: %12.2f\nPad with zeroes: %012.2f',...
5.2, 5.2)
txt =
'Pad with spaces:
5.20
Pad with zeroes: 000000005.20'
Note You can specify more than one flag in a formatting operator.
Value Identifiers
By default, functions such as sprintf insert values from input arguments into the output text in
sequential order. To process the input arguments in a nonsequential order, specify the order using
numeric identifiers in the format specifier. Specify nonsequential arguments with an integer
immediately following the % sign, followed by a $ sign.
Ordered Sequentially
Ordered By Identifier
sprintf('%s %s %s',...
'1st','2nd','3rd')
ans =
'1st 2nd 3rd'
sprintf('%3$s %2$s %1$s',...
'1st','2nd','3rd')
ans =
'3rd 2nd 1st'
Special Characters
Special characters can be part of the output text. But because they cannot be entered as ordinary
text, they require specific character sequences to represent them. To insert special characters into
output text, use any of the character sequences in the table.
Special Character
Representation in Format
Specifier
Single quotation mark
''
Percent character
%%
Backslash
\\
Formatting Text
6-27
