MATLAB Programming
MATLAB Programming MATLAB Programming
2 Data Types Note You can specify more than one flag in a formatting operator. Value Identifiers. By default, MATLAB inserts data values from the argument list into the string in a sequential order. If you have a need to use the value arguments in a nonsequential order, you can override the default by using a numeric identifier in each format specifier. Specify nonsequential arguments with an integer immediately following the % sign, followed by a $ sign. Ordered Sequentially sprintf('%s %s %s', ... '1st', '2nd', '3rd') ans = 1st 2nd 3rd Ordered By Identifier sprintf('%3$s %2$s %1$s', ... '1st', '2nd', '3rd') ans = 3rd 2nd 1st Setting Field Width and Precision This section provides further information on the use of the field width and precision fields of the formatting operator: • “Effect on the Output String” on page 2-52 • “Specifying Field Width and Precision Outside the format String” on page 2-53 • “Using Identifiers In the Width and Precision Fields” on page 2-54 Effect on the Output String. The figure below illustrates the way in which the field width and precision settings affect the output of the string formatting functions. In this figure, the zero following the % sign in the formatting operator means to add leading zeros to the output string rather than space characters: 2-52
Characters and Strings Whole part of input value has has 3 digits Result has w digits, extending to the left with zeros Fractional part of input value has 5 digits Format operator 123.45678 %09.3f 00123.457 field width: w = 9 precision: p = 3 Fractional part of the result has p digits and is rounded General rules for formatting • If precision is not specified, it defaults to 6. • If precision (p) is less than the number of digits in the fractional part of the input value (f), then only p digits are shown to the right of the decimal point in the output, and that fractional value is rounded. • If precision (p) is greater than the number of digits in the fractional part of the input value (f), then p digits are shown to the right of the decimal point in the output, and the fractional part is extended to the right with p-f zeros. • If field width is not specified, it defaults to precision + 1 + the number of digits in the whole part of the input value. • If field width (w) isgreaterthanp+1 plus the number of digits in the whole part of the input value (n), then the whole part of the output value is extended to the left with w-(n+1+p) space characters or zeros, depending on whether or not the zero flag is set in the Flags field. The default is to extend the whole part of the output with space characters. Specifying Field Width and Precision Outside the format String. To specify field width or precision using values from a sequential argument list, use an asterisk (*) inplaceofthefield width or precision field of the formatting operator. This example formats and displays three numbers. The formatting operator for the first, %*f, has an asterisk in the field width location of the formatting 2-53
- 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 148 and 149: 2 Data Types An alternate syntax, t
- Page 150 and 151: 2 Data Types Precision is not usual
- 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
- Page 198 and 199: 2 Data Types 3-by-3 Cell Array Note
- Page 200 and 201: 2 Data Types C5 = 'Jan' 'Feb' 'Mar'
Characters and Strings<br />
Whole part of input<br />
value has has 3 digits<br />
Result has w digits,<br />
extending to the<br />
left with zeros<br />
Fractional part of input<br />
value has 5 digits<br />
Format operator<br />
123.45678 %09.3f 00123.457<br />
field width: w = 9<br />
precision: p = 3<br />
Fractional part of the<br />
result has p digits<br />
and is rounded<br />
General rules for formatting<br />
• If precision is not specified, it defaults to 6.<br />
• If precision (p) is less than the number of digits in the fractional part of the<br />
input value (f), then only p digits are shown to the right of the decimal<br />
point in the output, and that fractional value is rounded.<br />
• If precision (p) is greater than the number of digits in the fractional part of<br />
the input value (f), then p digits are shown to the right of the decimal point<br />
in the output, and the fractional part is extended to the right with p-f zeros.<br />
• If field width is not specified, it defaults to precision + 1 + the number of<br />
digits in the whole part of the input value.<br />
• If field width (w) isgreaterthanp+1 plus the number of digits in the whole<br />
part of the input value (n), then the whole part of the output value is<br />
extended to the left with w-(n+1+p) space characters or zeros, depending<br />
on whether or not the zero flag is set in the Flags field. The default is to<br />
extend the whole part of the output with space characters.<br />
Specifying Field Width and Precision Outside the format String. To<br />
specify field width or precision using values from a sequential argument list,<br />
use an asterisk (*) inplaceofthefield width or precision field of the<br />
formatting operator.<br />
This example formats and displays three numbers. The formatting operator<br />
for the first, %*f, has an asterisk in the field width location of the formatting<br />
2-53