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

<strong>MATLAB</strong> Expressions<br />

Two specific types of <strong>MATLAB</strong> expressions covered in this section are<br />

• “String Evaluation” on page 3-28<br />

• “Shell Escape Functions” on page 3-29<br />

String Evaluation<br />

String evaluation adds power and flexibility to the <strong>MATLAB</strong> language, letting<br />

you perform operations like executing user-supplied strings and constructing<br />

executable strings through concatenation of strings stored in variables.<br />

eval<br />

The eval function evaluates a string that contains a <strong>MATLAB</strong> expression,<br />

statement, or function call. In its simplest form, the eval syntax is<br />

eval('string')<br />

For example, this code uses eval on an expression to generate a Hilbert<br />

matrix of order n.<br />

t = '1/(m + n - 1)';<br />

for m = 1:k<br />

for n = 1:k<br />

a(m,n) = eval(t);<br />

end<br />

end<br />

Here is an example that uses eval on a statement.<br />

eval('t = clock');<br />

Constructing Strings for Evaluation. You can concatenate strings to create<br />

a complete expression for input to eval. This code shows how eval can create<br />

10 variables named P1, P2, ..., P10, and set each of them to a different value.<br />

for n = 1:10<br />

eval(['P', int2str(n), '= n .^ 2'])<br />

end<br />

3-28

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

Saved successfully!

Ooh no, something went wrong!