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.

3 Basic Program Components<br />

Operator<br />

$<br />

${cmd}<br />

Usage<br />

The string represented by the token<br />

identified by name<br />

The string returned when <strong>MATLAB</strong><br />

executes the command cmd<br />

You can capture parts of the input string as tokens and then reuse them in<br />

the replacement string. Specify the parts of the string to capture using the<br />

token capture operator (...). Specify the tokens to use in the replacement<br />

string using the operators $1, $2, $N to reference the first, second, and Nth<br />

tokens captured. (See the section on “Tokens” on page 3-48 and the example<br />

“Using Tokens in a Replacement String” on page 3-53 in this documentation<br />

for information on using tokens.)<br />

Note When referring to a token within a replacement string, use the number<br />

of the token preceded by a dollar sign. For example, refer to token 2 by using<br />

$2, and not 2 or \2.<br />

The following example uses both the ${cmd} and $N operators in the<br />

replacement strings of nested regexprep commands to capitalize the first<br />

letter of each sentence. The inner regexprep looks for the start of the entire<br />

string and capitalizes the single instance; the outer regexprep looks for the<br />

first letter following a period and capitalizes the two instances:<br />

s1 = 'here are a few sentences.';<br />

s2 = 'none are capitalized.';<br />

s3 = 'let''s change that.';<br />

str = [s1 ' ' s2 ' ' s3]<br />

regexprep(regexprep(str, '(^.)', '${upper($1)}'), ...<br />

'(?

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

Saved successfully!

Ooh no, something went wrong!