MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 98 and 99: 1 Data Structures Functions to Dete
- Page 100 and 101: 1 Data Structures 1-76
- Page 102 and 103: 2 Data Types Cell Arrays (p. 2-94)
- Page 104 and 105: 2 Data Types Data Type Summary The
- Page 106 and 107: 2 Data Types Numeric Types Numeric
- Page 108 and 109: 2 Data Types the two equally nearby
- Page 110 and 111: 2 Data Types x = x = 127 -128 Also,
- Page 112 and 113: 2 Data Types Set the warning state
- Page 114 and 115: 2 Data Types Floating-Point Numbers
- Page 116 and 117: 2 Data Types isfloat(x) ans = 1 You
- Page 118 and 119: 2 Data Types In this example, 7.5 d
- Page 120 and 121: 2 Data Types 8.881784197001252e-016
- Page 122 and 123: 2 Data Types Note that the order of
- Page 124 and 125: 2 Data Types Complex Numbers Comple
- Page 126 and 127: 2 Data Types NaN MATLAB represents
- Page 128 and 129: 2 Data Types All available formats
- Page 130 and 131: 2 Data Types Integer Functions Func
- Page 132 and 133: 2 Data Types Infinity and NaN Funct
- Page 134 and 135: 2 Data Types 1 0 0 1 0 1 1 0 The MA
- Page 136 and 137: 2 Data Types A(B) ans = 5 15 30 35
- Page 138 and 139: 2 Data Types Characters and Strings
- Page 140 and 141: 2 Data Types name = ['Thomas R. Lee
- Page 142 and 143: 2 Data Types Phoenix length(strings
- Page 144 and 145: 2 Data Types • “Setting Field W
- Page 146 and 147: 2 Data Types Ordered Sequentially s
- Page 150 and 151: 2 Data Types Precision is not usual
- Page 152 and 153: 2 Data Types Note You can specify m
- Page 154 and 155: 2 Data Types operator, specifying t
- Page 156 and 157: 2 Data Types Valid Syntax Invalid S
- Page 158 and 159: 2 Data Types A = 'fate'; B = 'cake'
- Page 160 and 161: 2 Data Types while (any(remainder))
- Page 162 and 163: 2 Data Types Converting from String
- Page 164 and 165: 2 Data Types Function Summary MATLA
- Page 166 and 167: 2 Data Types Functions to Determine
- Page 168 and 169: 2 Data Types Date Strings There are
- Page 170 and 171: 2 Data Types • The day field is a
- Page 172 and 173: 2 Data Types datestr(d) ans = 01-Ma
- Page 174 and 175: 2 Data Types Utility Functions (Con
- Page 176 and 177: 2 Data Types • “Applying Functi
- Page 178 and 179: 2 Data Types where the arguments ar
- Page 180 and 181: 2 Data Types You can access subarra
- Page 182 and 183: 2 Data Types Dynamic Field Names Ex
- Page 184 and 185: 2 Data Types Deleting Fields from S
- Page 186 and 187: 2 Data Types plot(mercury, 'b') plo
- Page 188 and 189: 2 Data Types There are at least two
- Page 190 and 191: 2 Data Types Element-by-element org
- Page 192 and 193: 2 Data Types Nesting Structures A s
- Page 194 and 195: 2 Data Types Cell Arrays A cell arr
- Page 196 and 197: 2 Data Types Operation Syntax Descr
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