MATLAB Function Reference | ![]() ![]() |
Control the display format for output
Graphical Interface
As an alternative to format
, use preferences. Select Preferences from the File menu in the MATLAB desktop and use Command Window preferences.
Syntax
format formattype
format('type
')
Description
MATLAB performs all computations in double precision. Use the format
function to control the output format of the numeric values displayed in the Command Window. The format
function affects only how numbers are displayed, not how MATLAB computes or saves them. The specified format applies only to the current session. To maintain a format across sessions, use MATLAB preferences.
format
by itself, changes the output format to the default type, short
, which is 5-digit scaled, fixed-point values.
format
changes the format to the specified type
type
. The table below describes the allowable values for type
. To see the current type
file, use get(0,'Format')
, or for compact
versus loose
, use get(0,'FormatSpacing')
.
format('
is the function form of the syntax.type')
Examples
Change the format for pi
to long
by typing.
format long
pi
ans = 3.14159265358979
View the current format by typing
get(0,'Format')
ans = long
Set the format to short e
by typing
format short e
or use the function form of the syntax
format('short','e')
Algorithms
If the largest element of a matrix is larger than 103 or smaller than 10-3, 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
fprintf
, num2str
, rat
, sprintf
, spy
![]() | for | fplot | ![]() |