MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

NoteYou 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 \\
Alarm \a
Backspace \b
Form feed \f
New line \n
Carriage return \r
Horizontal tab \t
Vertical tab \v

Formatting Text
Free download pdf