23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3 Basic Program Components<br />

Operator<br />

(?(cond)expr)<br />

(?(cond)expr 1<br />

|expr 2<br />

)<br />

Usage<br />

If condition cond is true, then match expression<br />

expr<br />

If condition cond is true, then match expression<br />

expr 1<br />

. Otherwise match expression expr 2<br />

The first entry in this table is the same as an if-then statement. <strong>MATLAB</strong><br />

tests the state of condition cond and then matches expression expr only if<br />

the condition was found to be true. In the form of an if-then statement, it<br />

would look like this:<br />

if cond then expr<br />

The second entry in the table is the same as an if-then-else statement.<br />

If the condition is true, <strong>MATLAB</strong> matches expr 1<br />

; if false, it matches expr 2<br />

instead. This syntax is equivalent to the following programming statement:<br />

if cond then expr1 else expr2<br />

The condition cond in either of these syntaxes can be any one of the following:<br />

• A specific token, identified by either number or name, is located in the<br />

input string. See “Conditions Based on Tokens” on page 3-56, below.<br />

• A lookaround operation results in a match. See “Conditions Based on a<br />

Lookaround Match” on page 3-58, below.<br />

• A dynamic expression of the form (?@cmd) returns a nonzero numeric<br />

value. See “Conditions Based on Return Values” on page 3-58, below.<br />

Conditions Based on Tokens<br />

In a conditional expression, <strong>MATLAB</strong> matches the expression only if the<br />

condition associated with it is met. If the condition is based on a token,<br />

then the condition is met if <strong>MATLAB</strong> matches more than one character for<br />

the token in the input string.<br />

Tospecifyatokeninacondition,useeitherthetokennumberor,fortokens<br />

that you have assigned a name to, its name. Token numbers are determined<br />

by the order in which they appear in an expression. For example, if you<br />

3-56

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

Saved successfully!

Ooh no, something went wrong!