txt = sprintf('%1$*4$f
%2$.*5$f
%3$*6$.*7$f',...
123.45678, 16.42837, pi, 15, 3, 6, 4)
txt =
'
123.456780
16.428
3.1416'
The table describes the effect of each formatting operator in the example.
Formatting Operator
Description
%1$*4$f
1$ specifies the first input argument,
123.45678, as the value
*4$ specifies the fourth input argument, 15, as
the field width
%2$.*5$f
2$ specifies the second input argument,
16.42837, as the value
.*5$ specifies the fifth input argument, 3, as the
precision
%3$*6$.*7$f
3$ specifies the third input argument, pi, as the
value
*6$ specifies the sixth input argument, 6, as the
field width
.*7$ specifies the seventh input argument, 4, as
the precision
Restrictions on Using Identifiers
If any of the formatting operators include an identifier field, then all the operators in the format
specifier must include identifier fields. If you use both sequential and nonsequential ordering in the
same function call, then the output is truncated at the first switch between sequential and
nonsequential identifiers.
Valid Syntax
Invalid Syntax
sprintf('%d %d %d %d',...
1,2,3,4)
ans =
'1 2 3 4'
sprintf('%d %3$d %d %d',...
1,2,3,4)
ans =
'1 '
If your function call provides more input arguments than there are formatting operators in the format
specifier, then the operators are reused. However, only function calls that use sequential ordering
reuse formatting operators. You cannot reuse formatting operators when you use numbered
identifiers.
Valid Syntax
Invalid Syntax
sprintf('%d',1,2,3,4)
ans =
'1234'
sprintf('%1$d',1,2,3,4)
ans =
'1'
6 Characters and Strings
6-30
%2$.*5$f
%3$*6$.*7$f',...
123.45678, 16.42837, pi, 15, 3, 6, 4)
txt =
'
123.456780
16.428
3.1416'
The table describes the effect of each formatting operator in the example.
Formatting Operator
Description
%1$*4$f
1$ specifies the first input argument,
123.45678, as the value
*4$ specifies the fourth input argument, 15, as
the field width
%2$.*5$f
2$ specifies the second input argument,
16.42837, as the value
.*5$ specifies the fifth input argument, 3, as the
precision
%3$*6$.*7$f
3$ specifies the third input argument, pi, as the
value
*6$ specifies the sixth input argument, 6, as the
field width
.*7$ specifies the seventh input argument, 4, as
the precision
Restrictions on Using Identifiers
If any of the formatting operators include an identifier field, then all the operators in the format
specifier must include identifier fields. If you use both sequential and nonsequential ordering in the
same function call, then the output is truncated at the first switch between sequential and
nonsequential identifiers.
Valid Syntax
Invalid Syntax
sprintf('%d %d %d %d',...
1,2,3,4)
ans =
'1 2 3 4'
sprintf('%d %3$d %d %d',...
1,2,3,4)
ans =
'1 '
If your function call provides more input arguments than there are formatting operators in the format
specifier, then the operators are reused. However, only function calls that use sequential ordering
reuse formatting operators. You cannot reuse formatting operators when you use numbered
identifiers.
Valid Syntax
Invalid Syntax
sprintf('%d',1,2,3,4)
ans =
'1234'
sprintf('%1$d',1,2,3,4)
ans =
'1'
6 Characters and Strings
6-30
