Main Content

format

Set output display format for Command Window

Description

example

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

When you specify the style by name, you can usecommand formwithout parentheses or quotes:

formatshortG

example

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

example

fmt= format(style)stores the current display format infmtand 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 ofpi.

formatlongpi
ans = 3.141592653589793

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

formathexintmax('uint64')
ans =uint64ffffffffffffffff
realmax
ans = 7fefffffffffffff

Display the difference betweenshortEngandlongEngformats.

Set the output format toshortEng.

formatshortEng

创建一个变量and increase its value by a multiple of 10 each time through aforloop.

一个= 5.123456789;fork = 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.

formatlongEng一个= 5.123456789;fork = 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 theshortGformat when some of the values in an array are short numbers and some have large exponents. TheshortG格式选择哪个短fixed-decimal format or short scientific notation has the most compact display.

创建一个变量and display output in theshortformat, which is the default.

x = [25 56.31156 255.52675 9876899999]; formatshortx
x =1×4109× 0.0000 0.0000 0.0000 9.8769

Set the format toshortGand redisplay the values.

formatshortGx
x =1×40.0000 0.0000 0.0000 9.8769

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

formatshortEngformatcompactx = 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.

formatdefaultx
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 usingformatby 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 toshortEand display a 2-by-2 matrix of numeric values.

formatshortEm = [9638573934 37467; 236 574638295]
m =2×29.6386 0.0000 0.0000 0.5746

Save the current display format inoldFmtand change the numeric format tolongE.

oldFmt = format("longE")
oldFmt = DisplayFormatOptions with properties: NumericFormat: "shortE" LineSpacing: "loose"

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

m
m =2×29.6386 0.0000 0.0000 0.5746

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

format(oldFmt) m
m =2×29.6386 0.0000 0.0000 0.5746

Input Arguments

collapse all

Format to apply, specified as a character vector, string scalar, orDisplayFormatOptionsobject.

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

Default

defaultrestores the default display format, which isshortfor numeric format andloosefor 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 fordoublevalues, and 7 digits after the decimal point forsinglevalues.

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 fordoublevalues, and 7 digits after the decimal point forsinglevalues.

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 fordoublevalues, and 7 digits forsinglevalues.

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

TheDisplayFormatOptionsobject has two properties,NumericFormatandLineSpacing. The options for character vector and string scalar inputs are also the valid property values. For an example of using aDisplayFormatOptionsobject, seeSave and Restore Display Format.

Output Arguments

collapse all

Current display format, returned as aDisplayFormatOptionsobject with these properties:

  • NumericFormat

  • LineSpacing

For valid property values, see thestyleargument.

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. SeeSave and Restore Display Formatfor an example.

Tips

  • The specified format applies only to the current MATLAB session. To maintain a format across sessions, choose aNumeric formatorLine spacingoption in the Command Window preferences.

  • You can specifyshortorlongand the presentation type separately, such asformat short Eorformat("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 displayint8data types (for instance, -128:127). Setting the output format toshortorlongdoes 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 usingshortG. SeeLarge Data Range Format.

Compatibility Considerations

expand all

Not recommended starting in R2021a

Extended Capabilities

Introduced before R2006a