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 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

2 Data Types<br />

Note You can specify more than one flag in a formatting operator.<br />

Value Identifiers. By default, <strong>MATLAB</strong> inserts data values from the<br />

argument list into the string in a sequential order. If you have a need to use<br />

the value arguments in a nonsequential order, you can override the default<br />

by using a numeric identifier in each format specifier. Specify nonsequential<br />

arguments with an integer immediately following the % sign, followed by<br />

a $ sign.<br />

Ordered Sequentially<br />

sprintf('%s %s %s', ...<br />

'1st', '2nd', '3rd')<br />

ans =<br />

1st 2nd 3rd<br />

Ordered By Identifier<br />

sprintf('%3$s %2$s %1$s', ...<br />

'1st', '2nd', '3rd')<br />

ans =<br />

3rd 2nd 1st<br />

Setting Field Width and Precision<br />

This section provides further information on the use of the field width and<br />

precision fields of the formatting operator:<br />

• “Effect on the Output String” on page 2-52<br />

• “Specifying Field Width and Precision Outside the format String” on page<br />

2-53<br />

• “Using Identifiers In the Width and Precision Fields” on page 2-54<br />

Effect on the Output String. The figure below illustrates the way in<br />

which the field width and precision settings affect the output of the string<br />

formatting functions. In this figure, the zero following the % sign in the<br />

formatting operator means to add leading zeros to the output string rather<br />

than space characters:<br />

2-52

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

Saved successfully!

Ooh no, something went wrong!