23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2 Data Types<br />

operator, specifying that just the field width, 15, is to be taken from the<br />

argument list. The second operator, %.*f puts the asterisk after the decimal<br />

point meaning, that it is the precision that is to take its value from the<br />

argument list. And the third operator, %*.*f, specifies both field width and<br />

precision in the argument list:<br />

sprintf('%*f %.*f %*.*f', ...<br />

15, 123.45678, ... % Width for 123.45678 is 15<br />

3, 16.42837, ... % Precision for rand*20 is .3<br />

6, 4, pi) % Width & Precision for pi is 6.4<br />

ans =<br />

123.456780 16.428 3.1416<br />

You can mix the two styles. For example, this statement gets the field width<br />

from the argument list and the precision from the format string:<br />

sprintf('%*.2f', 5, 123.45678)<br />

ans =<br />

123.46<br />

Using Identifiers In the Width and Precision Fields. You can also<br />

derive field width and precision values from a nonsequential (i.e., numbered)<br />

argument list. Inside the formatting operator, specify field width and/or<br />

precision with an asterisk followed by an identifier number, followed by<br />

a $ sign.<br />

This example from the previous section showshowtoobtainfieldwidthand<br />

precision from a sequential argument list:<br />

sprintf('%*f %.*f %*.*f', ...<br />

15, 123.45678, ...<br />

3, 16.42837, ...<br />

6, 4, pi)<br />

ans =<br />

123.456780 16.428 3.1416<br />

Here is an example of how to do the same thing using numbered ordering.<br />

Field width for the first output value is 15, precision for the second value is<br />

3, and field width and precision for the thirdvalueis6and4,respectively.<br />

2-54

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

Saved successfully!

Ooh no, something went wrong!