Formatting Text - MATLAB & Simulink - MathWorks India
Formatting Text - MATLAB & Simulink - MathWorks India
Formatting Text
Functions That Format Data into Text
The following MATLAB functions offer the capability to compose character vectors or string arrays that include
ordinary text and data formatted to your specification:
The syntax of each of these functions includes formatting operators similar to those used by the printf function in
the C programming language. For example, %s tells MATLAB to interpret an input value as a character vector, and %d
means to format an integer using decimal notation.
The general formatting syntax for these functions is
functionname(..., formatSpec, value1, value2, ..., valueN)
where the formatSpec argument expresses the basic content and formatting of the final output, and the value
arguments that follow supply data values to be inserted into the character vector.
Here is a sample sprintf statement, also showing the resulting output:
sprintf('The price of %s on %d/%d/%d was $%.2f.', ...
'bread', 7, 1, 2006, 2.49)
ans =
The price of bread on 7/1/2006 was $2.49.
Note: The examples in this section of the documentation use only the sprintf function to demonstrate how to format text.
However, you can run the examples using the compose, fprintf, warning, and error functions as well.
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
Use . . .
1/8
10/27/2016
''
Percent character
%%
Backslash
\\
Alarm
\a
Backspace
\b
Form feed
\f
New line
\n
Carriage return
\r
Horizontal tab
\t
Vertical tab
\v
Hexadecimal number, N
\xN
Octal number, N
\N
Ordered By Identifier
sprintf('%3$s %2$s %1$s', ...
'1st', '2nd', '3rd')
ans =
3rd 2nd 1st
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
2/8
10/27/2016
Formatting operators tell MATLAB how to format the numeric or character value arguments and where to insert them
into the output text. These operators control the notation, alignment, significant digits, field width, and other aspects of
the output.
A formatting operator begins with a % character, which may be followed by a series of one or more numbers,
characters, or symbols, each playing a role in further defining the format of the insertion value. The final entry in this
series is a single conversion character that MATLAB uses to define the notation style for the inserted data.
Conversion characters used in MATLAB are based on those used by the printf function in the C programming
language.
Here is a simple example showing five formatting variations for a common value:
A = pi*100*ones(1,5);
sprintf(' %f \n %.2f
ans =
314.159265
314.16
+314.16
314.16
000000314.16
Precision Sets the number of digits to display to the right of the decimal point, or the number of significant digits
to display.
Flags Controls the alignment, padding, and inclusion of plus or minus signs.
Value Identifiers Map formatting operators to value input arguments. Use the identifier field when value
arguments are not in a sequential order in the argument list.
Here is an example of a formatting operator that uses all six fields. (Space characters are not allowed in the operator.
They are shown here only to improve readability of the figure).
An alternate syntax, that enables you to supply values for the field width and precision fields from values in the
argument list, is shown below. See the section Specifying Field Width and Precision Outside the Format Specifier for
information on when and how to use this syntax. (Again, space characters are shown only to improve readability of the
figure.)
Each field of the formatting operator is described in the following sections. These fields are covered as they appear
going from right to left in the formatting operator, starting with the Conversion Character and ending with the
Identifier field.
Conversion Character
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
3/8
10/27/2016
The conversion character specifies the notation of the output. It consists of a single character and appears last in the
format specifier. It is the only required field of the format specifier other than the leading % character.
Specifier
Description
Single character
Fixed-point notation
Character vector
This example uses conversion characters to display the number 46 in decimal, fixed-point, exponential, and
hexadecimal formats:
A = 46*ones(1,4);
sprintf('%d
%f
%e
%X', A)
ans =
46
46.000000
4.600000e+01
2E
Subtype
The subtype field is a single alphabetic character that immediately precedes the conversion character. To convert a
floating-point value to its octal, decimal, or hexadecimal value, use one of following subtype specifiers. These
subtypes support the conversion characters %o, %x, %X, and %u.
b
The underlying C data type is a double rather than an unsigned integer. For example, to print a double-precision
value in hexadecimal, use a format like '%bx'.
Precision
precision in a formatting operator is a nonnegative integer that immediately follows a period. For example, the
specifier %7.3f, has a precision of 3. For the %g specifier, precision indicates the number of significant digits to
display. For the %f, %e, and %E specifiers, precision indicates how many digits to display to the right of the decimal
point.
Here are some examples of how the precision field affects different types of notation:
sprintf('%g
%.2g
%f
%.2f', pi*50*ones(1,4))
ans =
157.08
1.6e+02
157.079633
157.08
Precision is not usually used in format specifiers for character vectors (i.e., %s). If you do use it on a character vector
and if the value p in the precision field is less than the number of characters in the vector, MATLAB displays only p
characters and truncates the rest.
You can also supply the value for a precision field from outside of the format specifier. See the section Specifying
Field Width and Precision Outside the Format Specifier for more information on this.
For more information on the use of precision in formatting, see Setting Field Width and Precision.
Field Width
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
4/8
10/27/2016
Field width in a formatting operator is a nonnegative integer that tells MATLAB the minimum number of digits or
characters to use when formatting the corresponding input value. For example, the specifier %7.3f, has a width of 7.
Here are some examples of how the width field affects different types of notation:
sprintf('|%e|%15e|%f|%15f|', pi*50*ones(1,4))
ans =
|1.570796e+02|
1.570796e+02|157.079633|
157.079633|
When used on a character vector, the field width can determine whether MATLAB pads the vector with spaces. If
width is less than or equal to the number of characters in the string, it has no effect.
sprintf('%30s', 'Pad left with spaces')
ans =
Pad left with spaces
You can also supply a value for field width from outside of the format specifier. See the section Specifying Field
Width and Precision Outside the Format Specifier for more information on this.
For more information on the use of field width in formatting, see Setting Field Width and Precision.
Flags
You can control the output using any of these optional flags:
Character
Description
Example
%-5.2d
%+5.2d
%+5s
A space ( )
% 5.2f
Zero (0)
%05.2f
%#5.0f
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
5/8
10/27/2016
Value Identifiers
By default, MATLAB inserts data values from the argument list into the output text in a sequential order. If you have a
need to use the value arguments in a nonsequential order, you can override the default by using a numeric identifier in
each format specifier. Specify nonsequential arguments with an integer immediately following the % sign, followed by a
$ sign.
Ordered Sequentially
sprintf('%s %s %s', ...
'1st', '2nd', '3rd')
ans =
1st 2nd 3rd
Ordered By Identifier
sprintf('%3$s %2$s %1$s', ...
'1st', '2nd', '3rd')
ans =
3rd 2nd 1st
If precision (p) is less than the number of digits in the fractional part of the input value (f), then only p digits are
shown to the right of the decimal point in the output, and that fractional value is rounded.
If precision (p) is greater than the number of digits in the fractional part of the input value (f), then p digits are
shown to the right of the decimal point in the output, and the fractional part is extended to the right with p-f zeros.
If field width is not specified, it defaults to precision + 1 + the number of digits in the whole part of the input
value.
If field width (w) is greater than p+1 plus the number of digits in the whole part of the input value (n), then the whole
part of the output value is extended to the left with w-(n+1+p) space characters or zeros, depending on whether or
not the zero flag is set in the Flags field. The default is to extend the whole part of the output with space
characters.
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
6/8
10/27/2016
%*.*f', ...
% Width for 123.45678 is 15
% Precision for rand*20 is .3
% Width & Precision for pi is 6.4
ans =
123.456780
16.428
3.1416
You can mix the two styles. For example, this statement gets the field width from the argument list and the precision
from the format specifier:
sprintf('%*.2f', 5, 123.45678)
ans =
123.46
Using Identifiers In the Width and Precision Fields
You can also derive field width and precision values from a nonsequential (i.e., numbered) argument list. Inside the
formatting operator, specify field width and/or precision with an asterisk followed by an identifier number, followed
by a $ sign.
This example from the previous section shows how to obtain field width and precision from a sequential argument list:
sprintf('%*f
%.*f
%*.*f', ...
15, 123.45678, ...
3, 16.42837, ...
6, 4, pi)
ans =
123.456780
16.428
3.1416
Here is an example of how to do the same thing using numbered ordering. Field width for the first output value is 15,
precision for the second value is 3, and field width and precision for the third value is 6 and 4, respectively. If you
specify field width or precision with identifiers, then you must specify the value with an identifier as well:
sprintf('%1$*4$f
%2$.*5$f
%3$*6$.*7$f', ...
123.45678, 16.42837, pi, 15, 3, 6, 4)
ans =
123.456780
16.428
3.1416
Invalid Syntax
sprintf('%d %3$d %d %d', ...
1, 2, 3, 4)
ans =
1
If your command provides more value arguments than there are formatting operators in the format specifier, MATLAB
reuses the operators. However, MATLAB allows this only for commands that use sequential ordering. You cannot
reuse formatting operators when making a function call with numbered ordering of the value arguments.
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
7/8
10/27/2016
Valid Syntax
sprintf('%d', 1, 2, 3, 4)
ans =
1234
Invalid Syntax
sprintf('%1$d', 1, 2, 3, 4)
ans =
1
Also, do not use identifiers when the value arguments are in the form of a vector or array:
Valid Syntax
Invalid Syntax
https://in.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
8/8