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.

4 M-File <strong>Programming</strong><br />

While the command syntax is simpler to write, it has the restriction that you<br />

may not assign any return values the function might generate. Attempting<br />

to do so generates an error.<br />

Two examples of command syntax are<br />

save mydata.mat x y z<br />

clear length width depth<br />

In the command syntax, <strong>MATLAB</strong> treats all arguments as string literals.<br />

<strong>MATLAB</strong> Function Syntax<br />

Function calls written in the function syntax look essentially the same as<br />

those in many other programming languages. One difference is that, in<br />

<strong>MATLAB</strong>, functions can return more than one output value.<br />

A function call with a single return value looks like this:<br />

out = functionname(in1, in2, ..., inN)<br />

If the function returns more than one value, separate the output variables<br />

with commas or spaces, and enclose them all in square brackets ([]):<br />

[out1, out2, ..., outN] = functionname(in1, in2, ..., inN)<br />

Here are two examples:<br />

copyfile(srcfile, '..\mytests', 'writable')<br />

[x1, x2, x3, x4] = deal(A{:})<br />

In the function syntax, <strong>MATLAB</strong> passes arguments to the function by value.<br />

See the examples under “Passing Arguments with Command and Function<br />

Syntax” on page 4-58.<br />

Passing Arguments with Command and Function Syntax<br />

When you call a function using function syntax, <strong>MATLAB</strong> passes the values<br />

assigned to each variable in the argument list. For example, this expression<br />

passes the values assigned to A0, A1, andA2 to the polyeig function:<br />

4-58

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

Saved successfully!

Ooh no, something went wrong!