附format的相关语句:
format
Set display format for output
Graphical Interface
As an alternative to using the format command, you can also use the
MATLAB Preferences GUI. Select File > Preferences
> Command Window and press the Help button for more
information.
Syntax
format
format type
format('type')
Description
Use the format function to control the output format of numeric
values displayed in the Command Window.
Note
format by itself, changes the output format to the default appropriate for the class of the variable currently being used. For floating-point variables, for example, the default is format short (i.e., 5-digit scaled, fixed-point values).
format type changes the format to the specified type. The tables shown below list the allowable values for type.
format('type') is the function form of the syntax.
The tables below show the allowable values for type, and provides an example for each type using pi.
Use these format types to switch between different output display formats for floating-point variables.
Type
Result
short
Scaled fixed point format, with 4 digits after the decimal point. For example, 3.1416.
long
Scaled fixed point format with 14 to 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, 3.141592653589793.
short e
Floating point format, with 4 digits after the decimal point. For example, 3.1416e+000.
long e
Floating point format, with 14 to 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, 3.141592653589793e+000.
short g
Best of fixed or floating point, with 4 digits after the decimal point. For example, 3.1416.
long g
Best of fixed or floating point, with 14 to 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, 3.14159265358979.
short eng
Engineering format that has 4 digits after the decimal point, and a power that is a multiple of three. For example, 3.1416e+000.
long eng
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 =
loose
Adds linefeeds to make output more readable. Contrast with compact.theta = pi/2
theta =
Remarks
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 (i.e., -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. Specifying Options for MATLAB Using 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 =
View the current format by typing
get(0,'format')
ans =
Set the format to short e by typing
format short e
or use the function form of the syntax
format('short','e')
Example 2
When the format is set to short, both pi and single(pi) display as
5-digit values:
format short
pi
ans =
single(pi)
ans =
Now set format to long, and pi displays a 15-digit value while
single(pi) display an 8-digit value:
format long
pi
ans =
single(pi)
ans =
Example 3
Set the format to its default, and display the maximum values for
integers and real numbers in MATLAB:
format
intmax('uint64')
ans =
realmax
ans =
Now change the format to hexadecimal, and display these same
values:
format hex
intmax('uint64')
ans =
realmax
ans =
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 short eng and long eng 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
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:
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.
已投稿到: |
![]() ![]() |
---|