format

Set Command Window output display format

Syntax

Description

example

format style changes the output display format in the Command Window to the format specified by style.

example

format, by itself, resets the output format to the default, which is the short, fixed-decimal format for floating-point notation and loose line spacing for all output lines.

Numeric formats affect only how numbers appear in Command Window output, not how MATLAB® computes or saves them.

Examples

expand all

Long Format

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

format long
pi
ans =

   3.141592653589793

Reset Format to Default

Set the output format to the short engineering format with compact line spacing, and then reset the format to the default.

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
format
x
x =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575

Hexadecimal Format

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

format hex
intmax('uint64')
ans =

   ffffffffffffffff

realmax
ans =

   7fefffffffffffff

Short and Long Engineering Notation

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.

Large Data Range Format

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 of short or shortE has the most compact display.

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

x = [25 56 255 9876899999];
format short
x
x =

   1.0e+09 *

    0.0000    0.0000    0.0000    9.8769

Set the format to shortG and redisplay the values.

format shortG
x
x =

           25           56          255   9.8769e+09

Get Current Format

Get the current numeric format.

f = get(0,'Format')
f =

shortG

Get the current line spacing, which can be set to loose or compact.

S = get(0,'FormatSpacing')
S =

loose

Input Arguments

expand all

style — Output display formatshort (default) | long | shortE | longE | ...

Output display format, specified by a string from the tables below.

Numeric Format

These styles control the output display format for numeric variables.

Style

Result

Example

short (default)

Short, fixed-decimal format with 4 digits after the decimal point.

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 or shortE, whichever is more compact.

3.1416

longG

long or longE, whichever is more compact.

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

rat

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.

theta = pi/2

theta =

  1.5708

More About

expand all

Tips

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

  • You can insert a space between short or long and the presentation type, for instance, 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.

See Also

| |

Was this topic helpful?