Main Content

format

Set output display format

Description

example

format(style) changes the output display format to the format specified by style. For example, format("shortG") displays numeric values in a compact form with 5 total digits. Numeric formats affect only how numbers appear in the display, not how MATLAB® computes or saves them.

When you specify the style by name, you can use command form without parentheses or quotes:

format shortG

example

fmt = format returns the current display format. (since R2021a)

example

fmt = format(style) stores the current display format in fmt and then changes the display format to the specified style. (since R2021a)

You cannot use command form when you request output or when you pass a variable as input. Enclose inputs in parentheses and include style names in quotes.

fmt = format("shortG");
format(fmt)

Examples

collapse all

Set the output format to the long fixed-decimal format and display the value of pi.

format long
pi
ans = 
   3.141592653589793

Display the maximum values for integers and real numbers in hexadecimal format.

format hex
intmax('uint64')
ans = uint64
   ffffffffffffffff

realmax
ans = 
   7fefffffffffffff

Display the difference between shortEng and longEng formats.

Set the output format to shortEng.

format shortEng

Create a variable and increase its value by a multiple of 10 each time through a for loop.

A = 5.123456789;
for k = 1:10
   disp(A)
   A = A*10;
end
     5.1235e+000

    51.2346e+000

   512.3457e+000

     5.1235e+003

    51.2346e+003

   512.3457e+003

     5.1235e+006

    51.2346e+006

   512.3457e+006

     5.1235e+009

The values display with 4 digits after the decimal point and an exponent that is a multiple of 3.

Set the output format to the long engineering format and view the same values.

format longEng

A = 5.123456789;
for k = 1:10
   disp(A)
   A = A*10;
end
    5.12345678900000e+000

    51.2345678900000e+000

    512.345678900000e+000

    5.12345678900000e+003

    51.2345678900000e+003

    512.345678900000e+003

    5.12345678900000e+006

    51.2345678900000e+006

    512.345678900000e+006

    5.12345678900000e+009

The values display with 15 digits and an exponent that is a multiple of 3.

Use the shortG format when some of the values in an array are short numbers and some have large exponents. The shortG format picks whichever short fixed-decimal format or short scientific notation has the most compact display.

Create a variable and display output in the short format, which is the default.

x = [25 56.31156 255.52675 9876899999];
format short
x
x = 1×4
109 ×

    0.0000    0.0000    0.0000    9.8769

Set the format to shortG and redisplay the values.

format shortG
x
x = 1×4

           25       56.312       255.53   9.8769e+09

Set the output format to the short engineering format with compact line spacing.

format shortEng
format compact
x = rand(3)
x =
   814.7237e-003   913.3759e-003   278.4982e-003
   905.7919e-003   632.3592e-003   546.8815e-003
   126.9868e-003    97.5404e-003   957.5068e-003

Reset the display format to default and display the matrix again.

format default
x
x =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575

Before R2021a, reset the display format to default values using format by itself.

format

Since R2021a

Get the current display format.

fmt = format
fmt = 
  DisplayFormatOptions with properties:

    NumericFormat: "short"
      LineSpacing: "loose"

Since R2021a

Save the current display format and restore it at a later time.

Set the numeric display to shortE and display a 2-by-2 matrix of numeric values.

format shortE
m = [9638573934 37467; 236 574638295]
m = 2×2

   9.6386e+09   3.7467e+04
   2.3600e+02   5.7464e+08

Save the current display format in oldFmt and change the numeric format to longE.

oldFmt = format("longE")
oldFmt = 
  DisplayFormatOptions with properties:

    NumericFormat: "shortE"
      LineSpacing: "loose"

Confirm that the numeric format is now long, scientific notation by redisplaying matrix m.

m
m = 2×2

     9.638573934000000e+09     3.746700000000000e+04
     2.360000000000000e+02     5.746382950000000e+08

Restore the format to its previous state. Redisplay m to confirm that the numeric format is now short, scientific format.

format(oldFmt)
m
m = 2×2

   9.6386e+09   3.7467e+04
   2.3600e+02   5.7464e+08

Input Arguments

collapse all

Format to apply, specified as a character vector, string scalar, or DisplayFormatOptions object.

Character vectors or string scalars must be one of the listed style names or default.

Default

default restores the default display format, which is short for numeric format and loose for line spacing. (since R2021a)

Numeric Format

These styles control the output display format for numeric variables.

Style

Result

Example

short

Short, fixed-decimal format with 4 digits after the decimal point. This is the default numeric setting.

3.1416

long

Long, fixed-decimal format with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.

3.141592653589793

shortE

Short scientific notation with 4 digits after the decimal point.

3.1416e+00

longE

Long scientific notation with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.

3.141592653589793e+00

shortG

Short, fixed-decimal format or scientific notation, whichever is more compact, with a total of 5 digits.

3.1416

longG

Long, fixed-decimal format or scientific notation, whichever is more compact, with a total of 15 digits for double values, and 7 digits for single values.

3.14159265358979

shortEng

Short engineering notation (exponent is a multiple of 3) with 4 digits after the decimal point.

3.1416e+000

longEng

Long engineering notation (exponent is a multiple of 3) with 15 significant digits.

3.14159265358979e+000

+

Positive/Negative format with +, -, and blank characters displayed for positive, negative, and zero elements.

+

bank

Currency format with 2 digits after the decimal point.

3.14

hex

Hexadecimal representation of a binary double-precision number.

400921fb54442d18

rational

Ratio of small integers.

355/113

Line Spacing Format

Style

Result

Example

compact

Suppress excess blank lines to show more output on a single screen.

theta = pi/2
theta =
  1.5708

loose

Add blank lines to make output more readable. This is the default setting for line spacing.

theta = pi/2

theta =

  1.5708

The DisplayFormatOptions object has two properties, NumericFormat and LineSpacing. The options for character vector and string scalar inputs are also the valid property values. For an example of using a DisplayFormatOptions object, see Save and Restore Display Format.

Output Arguments

collapse all

Current display format, returned as a DisplayFormatOptions object with these properties:

  • NumericFormat

  • LineSpacing

For valid property values, see the style argument.

Note

Property values reflect the state of the display format when the object is created. The properties do not automatically change when the display format changes. See Save and Restore Display Format for an example.

Tips

  • The specified format applies only to the current MATLAB session. To maintain a format across sessions, choose a Numeric format or Line spacing option in the Command Window preferences.

  • You can specify short or long and the presentation type separately, such as format short E or format("short E").

  • MATLAB always displays integer data types to the appropriate number of digits for the data type. For example, MATLAB uses 3 digits to display int8 data types (for instance, -128:127). Setting the output format to short or long does not affect the display of integer-type variables.

  • Integer-valued, floating-point numbers with a maximum of 9 digits do not display in scientific notation.

  • If you are displaying a matrix with a wide range of values, consider using shortG. See Large Data Range Format.

Extended Capabilities

Version History

Introduced before R2006a

expand all