MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Subtype

The subtype field is a single alphabetic character that immediately precedes the
conversion character. Without the subtype field, the conversion characters %o, %x, %X,
and %u treat input data as integers. To treat input data as floating-point values instead
and convert them to octal, decimal, or hexadecimal representations, use one of following
subtype specifiers.

b The input data are double-precision floating-point values rather than unsigned
integers. For example, to print a double-precision value in hexadecimal, use a
format like %bx.
t The input data are single-precision floating-point values rather than unsigned
integers.

Precision

The precision field in a formatting operator is a nonnegative integer that immediately
follows a period. For example, in the operator %7.3f, the precision is 3. For the %g
operator, the precision indicates the number of significant digits to display. For the %f,
%e, and %E operators, the precision indicates how many digits to display to the right of
the decimal point.

Display numbers to different precisions using the precision field.

txt = sprintf('%g %.2g %f %.2f', pi*50*ones(1,4))

txt =
'157.08 1.6e+02 157.079633 157.08'

While you can specify the precision in a formatting operator for input text (for example, in
the %s operator), there is usually no reason to do so. If you specify the precision as p, and
p is less than the number of characters in the input text, then the output contains only the
first p characters.

Field Width

The field width in a formatting operator is a nonnegative integer that specifies the
number of digits or characters in the output when formatting input values. For example,
in the operator %7.3f, the field width is 7.

6 Characters and Strings

Free download pdf