Wednesday, August 5, 2009

format

Control 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
    format type
    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, instead use MATLAB preferences.

format by itself, changes the output format to the default type, short, which is 5-digit scaled, fixed-point values.

format type changes the format to the specified type. The table below describes the allowable values for type and provides an example for pi, unless otherwise noted. To see the current type file, use get(0,'Format'), or for compact versus loose, use get(0,'FormatSpacing').


Value for type
Result
Example
+
+, -, blank
+
bank
Fixed dollars and cents
3.14
compact
Suppresses excess line feeds to show more output in a single screen. Contrast with loose.
theta = pi/2
theta=
1.5708
hex
Hexadecimal (hexadecimal representation of a binary double-precision number)
400921fb54442d18
long
15-digit scaled fixed point
3.14159265358979
long e
15-digit floating point
3.141592653589793e+00
long g
Best of 15-digit fixed or floating point
3.14159265358979
loose
Adds linefeeds to make output more readable. Contrast with compact.
theta = pi/2

theta=

1.5708
rat
Ratio of small integers
355/113
short
5-digit scaled fixed point
3.1416
short e
5-digit floating point
3.1416e+00
short g
Best of 5-digit fixed or floating point
3.1416

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

Examples

Change the format to long by typing

  • format long

View the result for the value of pi by typing

  • pi

and MATLAB returns

  • ans =
    3.14159265358979

View the current format by typing

  • get(0,'Format')

MATLAB returns

  • 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.

No comments:

Post a Comment