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

Precision is not usually used in format specifiers for strings (i.e., %s). If you<br />

do use it on a string and if the value p in the precision field is less than the<br />

number of characters in the string, <strong>MATLAB</strong> displays only p characters of the<br />

string and truncates the rest.<br />

You can also supply the value for a precision field from outside of the format<br />

specifier. See the section “Specifying Field Width and Precision Outside the<br />

format String” on page 2-53 for more information on this.<br />

For more information on the use of precision in formatting, see “Setting<br />

Field Width and Precision” on page 2-52.<br />

Field Width. Field width in a formatting operator is a nonnegative integer<br />

that tells <strong>MATLAB</strong> the minimum number of digits or characters to use when<br />

formatting the corresponding input value. For example, the specifier %7.3f,<br />

has a width of 7.<br />

Herearesomeexamplesofhowthewidth field affects different types of<br />

notation:<br />

sprintf('|%e|%15e|%f|%15f|', pi*50*ones(1,4))<br />

ans =<br />

|1.570796e+002| 1.570796e+002|157.079633| 157.079633|<br />

When used on a string, the field width can determine whether <strong>MATLAB</strong><br />

pads the string with spaces. If width is less than or equal to the number of<br />

characters in the string, it has no effect.<br />

sprintf('%30s', 'Pad left with spaces')<br />

ans =<br />

Pad left with spaces<br />

You can also supply a value for field width from outside of the format<br />

specifier. See the section “Specifying Field Width and Precision Outside the<br />

format String” on page 2-53 for more information on this.<br />

For more information on the use of field width in formatting, see “Setting<br />

Field Width and Precision” on page 2-52.<br />

2-50

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

Saved successfully!

Ooh no, something went wrong!