Skip to Main Content Skip to Search
Product Documentation

format - Set display format for output

Alternatives

As an alternative to using the format command, you also can use the MATLAB Preferences GUI. For more information, select File > Preferences > Command Window and press the Help button.

Syntax

format
format type
format('type')

Description

Use the format function to control the output format of numeric values displayed in the Command Window.

format by itself specifies the default display type, format short (that is, 5-digit scaled, fixed-point values).

format type changes the format to the specified type. The following tables list the allowable values for type.

format('type') is the function form of the syntax.

The following tables show the allowable values for type, and provide an example for each type using pi. Use these format types to switch between different output display formats for floating-point variables.

The tables show each type specified as a single string. If you prefer, you can insert a space between short or long and the presentation type, for instance, format short e or format('long', 'eng'). Types are case-insensitive.

Type

Result

short (default)

Scaled fixed-point format, with 4 digits after the decimal point. For example, 3.1416.

If you are displaying a matrix with a wide range of values, consider using shortG. See Example 5.

long

Scaled fixed-point format with 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, 3.141592653589793.

shortE

Floating-point format, with 4 digits after the decimal point. For example, 3.1416e+00.

Integer-valued floating-point numbers with a maximum of 9 digits are not displayed in scientific notation.

longE

Floating-point format, with 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, 3.141592653589793e+00.

Integer-valued floating-point numbers with a maximum of 9 digits are not displayed in scientific notation.

shortG

Fixed- or floating-point, whichever is more readable, with 4 digits after the decimal point. For example, 3.1416.

See Example 5, for a comparison between this format and short.

longG

Fixed- or floating-point, whichever is more readable, with 14 to 15 digits after the decimal point for double; and 6 or 7 digits after the decimal point for single. For example, 3.14159265358979.

shortEng

Engineering format that has 4 digits after the decimal point, and a power that is a multiple of three. For example, 3.1416e+000.

longEng

Engineering format that has exactly 16 significant digits and a power that is a multiple of three. For example, 3.14159265358979e+000.

Use these format types to switch between different output display formats for all numeric variables.

Value for type

Result

+

+, -, blank

bank

Fixed dollars and cents. For example, 3.14

hex

Hexadecimal (hexadecimal representation of a binary double-precision number). For example, 400921fb54442d18

rat

Ratio of small integers. For example, 355/113

Use these format types to affect the spacing in the display of all variables.

Value for type

Result

Example

compact

Suppresses excess line feeds to show more output in a single screen. Contrast with loose.

theta = pi/2
theta =
  1.5708

loose

Adds linefeeds to make output more readable. Contrast with compact.

theta = pi/2

theta =

  1.5708

Tips

Computations on floating-point variables, namely single or double, are done in appropriate floating-point precision, no matter how those variables are displayed. Computations on integer variables are done natively in integer.

MATLAB always displays integer variables to the appropriate number of digits for the class. For example, MATLAB uses three digits to display numbers of type int8 (for example, -128:127). Setting format to short or long does not affect the display of integer variables.

The specified format applies only to the current MATLAB session. To maintain a format across sessions, use MATLAB preferences.

To see which type is currently in use, type

get(0,'Format')

To see if compact or loose formatting is currently selected, type

get(0,'FormatSpacing')

Examples

Example 1

Change the format to long by typing

format long

View the result for the value of pi by typing

pi
ans =
   3.141592653589793

View the current format by typing

get(0,'format')
ans =
   long

Set the format to shortE by typing

format shortE

or use the function form of the syntax

format('shortE')

Example 2

When you set the format to short, both pi and single(pi) each display as a 5-digit value:

format short

pi
ans =
    3.1416

single(pi)
ans =
    3.1416

Set format to long. Now, pi displays a 15-digit value while single(pi) displays an 8-digit value:

format long

pi
ans =
    3.141592653589793

single(pi)
ans =
    3.1415927

Example 3

Set the format to its default, and display the maximum values for integers and real numbers in MATLAB:

format

intmax('uint64')
ans =
   18446744073709551615

realmax
ans =
  1.7977e+308

Now change the format to hexadecimal, and display these same values:

format hex

intmax('uint64')
ans =
   ffffffffffffffff

realmax
ans =
   7fefffffffffffff

The hexadecimal display corresponds to the internal representation of the value. It is not the same as the hexadecimal notation in the C programming language.

Example 4

This example illustrates the shortEng and longEng formats. The value assigned to variable A increases by a multiple of 10 each time through the for loop.

A = 5.123456789;

for k=1:10
   disp(A)
   A = A * 10;
end

The values displayed for A are shown here. The power of 10 is always a multiple of 3. The value itself is expressed in 5 or more digits for the short eng format, and in exactly 15 digits for long eng:

  format shortEng                format longEng

     5.1235e+000                5.12345678900000e+000
    51.2346e+000               51.2345678900000e+000
   512.3457e+000              512.345678900000e+000
     5.1235e+003                5.12345678900000e+003
    51.2346e+003               51.2345678900000e+003
   512.3457e+003              512.345678900000e+003
     5.1235e+006                5.12345678900000e+006
    51.2346e+006               51.2345678900000e+006
   512.3457e+006              512.345678900000e+006
     5.1235e+009                5.12345678900000e+009

Example 5

This example illustrates the difference between using shortG and the default type, short, when the values in a matrix cover a wide range.

Define x as follows:

x = [25 56 255 9876899999]

When the format is short, the Command Window display appears as follows. Notice that it indicates each value is multiplied by 1.0e+09.

x =

  1.0e+09 *

  0.0000    0.0000    0.0000    9.8769

Set the format to shortG and redisplay x:

format shortG
x

Notice that the Command Window display is now:

x =

   25           56          255  9.8769e+09

Algorithms

If the largest element of a matrix is larger than 103 or smaller than 10-3, then MATLAB applies a common scale factor for the short and long formats. The function format + displays +, -, and blank characters for positive, negative, and zero elements. format hex displays the hexadecimal representation of a binary double-precision number. format rat uses a continued fraction algorithm to approximate floating-point values by ratios of small integers. See rat.m for the complete code.

See Also

disp | display | floor | fprintf | isfloat | isinteger | isnumeric | num2str | rat | sprintf | spy

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS