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.

Characters and Strings<br />

If you specify field width or precision with identifiers, then you must specify<br />

the value with an identifier as well:<br />

sprintf('%1$*4$f %2$.*5$f %3$*6$.*7$f', ...<br />

123.45678, 16.42837, pi, 15, 3, 6, 4)<br />

ans =<br />

123.456780 16.428 3.1416<br />

Restrictions for Using Identifiers<br />

If any of the formatting operators in a string include an identifier field, then<br />

all of the operators in that string must do the same; you cannot use both<br />

sequential and nonsequential ordering in the same function call.<br />

Valid Syntax<br />

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

1, 2, 3, 4)<br />

ans =<br />

1 2 3 4<br />

Invalid Syntax<br />

sprintf('%d %3$d %d %d', ...<br />

1, 2, 3, 4)<br />

ans =<br />

1<br />

If your command provides more value arguments than there are formatting<br />

operators in the format string, <strong>MATLAB</strong> reuses the operators. However,<br />

<strong>MATLAB</strong> allows this only for commands that use sequential ordering.<br />

You cannot reuse formatting operators when making a function call with<br />

numbered ordering of the value arguments.<br />

Valid Syntax<br />

sprintf('%d', 1, 2, 3, 4)<br />

ans =<br />

1234<br />

Invalid Syntax<br />

sprintf('%1$d', 1, 2, 3, 4)<br />

ans =<br />

1<br />

Also, do not use identifiers when the value arguments are in the form of a<br />

vector or array:<br />

2-55

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

Saved successfully!

Ooh no, something went wrong!