MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

2 Data Types An alternate syntax, that enables you to supply values for the field width and precision fields from values in the argument list, is shown below. See the section “Specifying Field Width and Precision Outside the format String” on page 2-53 for information on when and how to use this syntax. (Again, space characters are shown only to improve readability of the figure.) Each field of the formatting operator is described in the following sections. These fields are covered as they appear going from right to left in the formatting operator, starting with the Conversion Character and ending with the Identifier field. Conversion Character. The conversion character specifies the notation of the output. It consists of a single character and appears last in the format specifier. It is the only required field of the format specifier other than the leading % character. Specifier c d e E f g G o s u Description Single character Decimal notation (signed) Exponential notation (using a lowercase e as in 3.1415e+00) Exponential notation (using an uppercase E as in 3.1415E+00) Fixed-point notation Themorecompactof%e or %f. (Insignificant zeros do not print.) Same as %g, but using an uppercase E Octal notation (unsigned) String of characters Decimal notation (unsigned) 2-48

Characters and Strings Specifier x X Description Hexadecimal notation (using lowercase letters a–f) Hexadecimal notation (using uppercase letters A–F) This example uses conversion characters to display the number 46 in decimal, fixed-point, exponential, and hexadecimal formats: A = 46*ones(1,4); sprintf('%d %f %e %X', A) ans = 46 46.000000 4.600000e+001 2E Subtype. The subtype field is a single alphabetic character that immediately precedes the conversion character. The following nonstandard subtype specifiers are supported for the conversion characters %o, %u, %x, and%X. b t The underlying C data type is a double rather than an unsigned integer. For example, to print a double-precision value in hexadecimal, use a format like '%bx'. The underlying C data type is a float rather than an unsigned integer. Precision. precision in a formatting operator is a nonnegative integer that tells MATLAB how many digits to the right of the decimal point to use when formatting the corresponding input value. The precision field consists of a nonnegative integer that immediately follows a period and extends to the first alphabetic character after that period. For example, the specifier %7.3f, has a precision of 3. Here are some examples of how the precision field affects different types of notation: sprintf('%g %.2g %f %.2f', pi*50*ones(1,4)) ans = 157.08 1.6e+002 157.079633 157.08 2-49

Characters and Strings<br />

Specifier<br />

x<br />

X<br />

Description<br />

Hexadecimal notation (using lowercase letters a–f)<br />

Hexadecimal notation (using uppercase letters A–F)<br />

This example uses conversion characters to display the number 46 in decimal,<br />

fixed-point, exponential, and hexadecimal formats:<br />

A = 46*ones(1,4);<br />

sprintf('%d %f %e %X', A)<br />

ans =<br />

46 46.000000 4.600000e+001 2E<br />

Subtype. The subtype field is a single alphabetic character that immediately<br />

precedes the conversion character. The following nonstandard subtype<br />

specifiers are supported for the conversion characters %o, %u, %x, and%X.<br />

b<br />

t<br />

The underlying C data type is a double rather than an unsigned<br />

integer. For example, to print a double-precision value in<br />

hexadecimal, use a format like '%bx'.<br />

The underlying C data type is a float rather than an unsigned integer.<br />

Precision. precision in a formatting operator is a nonnegative integer that<br />

tells <strong>MATLAB</strong> how many digits to the right of the decimal point to use when<br />

formatting the corresponding input value. The precision field consists of a<br />

nonnegative integer that immediately follows a period and extends to the<br />

first alphabetic character after that period. For example, the specifier %7.3f,<br />

has a precision of 3.<br />

Here are some examples of how the precision field affects different types<br />

of notation:<br />

sprintf('%g %.2g %f %.2f', pi*50*ones(1,4))<br />

ans =<br />

157.08 1.6e+002 157.079633 157.08<br />

2-49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!