Formatting Text

Data is saved in the form of arrays. When we display the data, it comes in the form of an array, and that may not be as representable as always. So, there are some formatting operators available in the MATLAB to convert the data to text and format the output as per our requirements.

  • Formatting operators control notation, alignment, significant digits of the output.
  • We can combine formatting operators with normal text and special characters in a format specifier.
  • Formatting operators can be used with the functions: compose, num2str, sprintf, fprintf, assert, error, warning, and MException.

Example:

Output:

>> t

t =
  'Displaying 3 random numbers: 
      1)0.957167 
      2)0.49 
      3)0.800280'

Fields of the Formatting Operator in MATLAB

The % percent sign always leads the formatting operator. A formatting operator can have a maximum of six fields-the conversion character, subtype, precision, field width, flags, and numeric identifier.

  • The conversion character is an only required field among all six fields.
  • The conversion character must always lead by a % percent sign.
  • And no space character is allowed in between the fields of formatting operator.

Syntax Example:

Formatting Text

Conversion Character

The conversion character is an only required field, and it specifies the notation of the output. The conversion character is denoted by a single alphabetical character and appears last in the format specifier.

Sl. No.Conversion characterDescription
1.cSingle character
2.dDecimal notation (signed)
3.eExponential notation (using a lowercase e, as in 3.1415e+00)
4.EExponential notations (using a uppercase E, as in 3.1415E+00)
5.fFixed-point notation
6.gThe more compact of the %e or %f (insignificant zero do not print)
7.GSimilar to %g, but using an uppercase E
8.oOctal notation (unsigned)
9.sCharacter vector or string array
10.uDecimal notation (unsigned)
11.xHexadecimal notation (unsigned, using lowercase letters a-f)
12.XHexadecimal notation (unsigned, using lowercase letters A-F)

Example:

Output:

>> res
res = 

    "c		d 
     d		100 
     e		1.000000e+02 
     E		1.000000E+02
     f		100.000000 
     g		100 
     G		100 
     o		144
     s		d
     u		100
     x		64
     X		64"

Subtype

The subtype field is represented by a single alphabetic character that comes immediately before the conversion character. The conversion characters %o, %u, %x, and %X treat data input as integers, without the subtype field. So, use one of the following subtype specifiers to treat data input as floating-point values and convert them to octal, decimal, or hexadecimal representations.

Subtype specifierDescription
bTreats input data as double-precision floating-point values rather than unsigned integers.
tTreats input data as single-precision floating-point values rather than unsigned integers.

Example:

Output:

>> res

res =

    'before using subtype:	100
     after using subtype:	1120403456'

Precision

The precision field is denoted by a non-negative integer that immediately comes after a period (dot). It is used with %f, %e, and %E operators and indicates the number of digits to display to the right of the decimal point.

Example:

Output:

>> res

res =

    'output without and with precision field:   94.247780  94.25  9.424778e+01  9.42e+01'

Field Width

The field width is a non-negative integer and comes with or without decimal point precision. As the precision field specifies the number of digits after the decimal point, the field width specifies how many numbers of total characters should be displayed in the output. If the field width is higher than the number of characters in the input data, then by default, the output text is padded with space characters.

Example:

Output:

>> res
res =
   'output without and with field width:  |3.033000e+02|  |   3.033000e+02|	|303.300000|  |   303.300000|'

Flags

The flags field controls the additional formatting of the output. The characters used for the flags describe mainly the spacing, padding, and text alignment.

Flags characterDescriptionExample
Minus sign (-)Left-justify the output%-5.2d
Plus sign (+)Right-justify the output if the output is a text, and add a leading plus sign character if it is number%+5.2d
%+5s
SpaceInserts space before the output% 5.2f
Zero (0)Pad the output with zero instead of space%05.2f
The pound sign (#)The pound sign flag is a special case as it is somehow different from other flags. It modifies selected numeric conversions as:
  • Prints 0, 0x, or 0X prefix for %o, %x, or %X operators.
  • Prints decimal point even when precision is 0 for %f, %e, or %E operators.
  • It doesn't remove trailing zeroes or decimal points for %g or %G operators.
%#5.0f

Example: -Right and left justify:

Output:

>> ans

ans =

    'right-justify:|       flags|
     left-justify:|10.00       |'

Example: - Padding with space and zero:

Output:

>> b

b =

    'padding with space: |  20.00|
      padding with zero: |0000020.00|'

Example: -Pound Sign:

Output:

>> a

a =

    '10.'

Identifiers

The output functions such as sprintf prints the output in the same sequence as it takes input arguments. So, use the identifier to produce the output in a custom specified order. Identifiers are the integer values and come immediately after the % percent sign following by a $ dollar sign.

Default order

Example:

Output:

>> t1

t1 =

    '1st 2nd 3rd'

Custom order using identifier

Example:

Output:

>> t2

t2 =

    '3rd 1st 2nd'

Displaying Special Characters in the Output

Special characters are meant to serve particular purposes, so these cannot be used as ordinary text. To make special characters as part of the output text, use specific character sequences.

Special characterCharacter sequence
Single quotation mark'' (single quote two times)
%%
Backslash\\
Alarm\a
Backspace\b
Form feed\f
New line\n
Carriage return\r
Horizontal tab\t
Vertical tab\v
Representation of Unicode numeric value of the hexadecimal number, N\xN
Example: sprintf('\x6A') returns character 'j'
Representation of Unicode numeric value of the octal number, N\N
Example: sprintf('\100') returns character '@'

Rules for setting Field Width and Precision

The formatting operators follow specific rules for formatting the output text. And field width & precision define how a number should be displayed in the output. Let's examine with an illustrated example:

Formatting Text
  • If precision is not specified, then the default value is set to six.
  • If precision p is less than the number of a digit in the fractional part of the input, then only p digits are displayed after the decimal point, and the output value is rounded off.
  • If the precision p is higher than the no. of digits in the fractional part, then the fractional part is shown with added zeroes to the right of p digits.
  • If the field width w is not specified, then the default value is calculated as per p+1+n, n is the number of digits in the whole part of an input value.
  • If the field width w is higher than p+1+n, then the whole part of the output is padded to the left with either space or zero additional characters, calculated as w-(p+1+n).

Field Width and Precision, Outside the Format Specifier

To specify the field width and precision outside the format specifier, use an asterisk (*) in place of the field width or precision fields of the formatting operator. The place of the asterisk should match the place of the number specifying the fields in the input arguments. Lets' understand with an example:

Example:

Output:

>> t3

t3 =

    ?123.456000 10.235  3.14?

Explanation of the above example:

Formatting OperatorDescription
%*fSpecify field width as the following input argument, 10.
%.*fSpecify precision as the next input argument, 3.
%*.*fSpecify width and precision as the next input arguments, 4, and 2.

Specifying Identifiers in place of Field Width and Precision

We can specify numbered identifiers for field width and precision, and the value for the identifier comes from input arguments.

Formatting Text

Example:

Output:

>>t4 =

    '123.456000  12.3659  3.14'

Lets' examine the above example:

Formatting operatorDescription
%1$*4$f1$ specifies the first input argument, 123.456, as the value
*4$ specifies the fourth input argument, 10, as the field width
%2$.*5$f2$ specifies the second input argument, 12.36587, as the value
.*5$ specifies the fifth input argument, 4, as the precision
%3$*6$.*7$f3$ specifies the third input argument, 3.145678, as the value
*6$ specifies the sixth input argument, 4, as the field width
.*7$ specifies the seventh input argument, 2, as the precision