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.

Function Arguments<br />

varargout(k) = {s(k)};<br />

end<br />

Try calling it with<br />

[s, rows, cols] = mysize(rand(4, 5))<br />

String or Numeric Arguments<br />

If you are passing only string arguments into a function, you can use<br />

<strong>MATLAB</strong> command syntax. All arguments entered in command syntax are<br />

interpreted as strings.<br />

strcmp string1 string1<br />

ans =<br />

1<br />

When passing numeric arguments, it is best to use function syntax unless you<br />

want the number passed as a string. The right-hand example below passes<br />

the number 75 as the string, '75'.<br />

isnumeric(75) isnumeric 75<br />

ans = ans =<br />

1 0<br />

For more information: See “Passing Arguments with Command and<br />

Function Syntax” on page 4-58 in the <strong>MATLAB</strong> <strong>Programming</strong> documentation.<br />

Passing Arguments in a Structure<br />

Instead of requiring an additional argument for every value you want to pass<br />

in a function call, you can package them in a <strong>MATLAB</strong> structure and pass the<br />

structure. Make each input you want to pass a separate field in the structure<br />

argument, using descriptive names for the fields.<br />

Structures allow you to change the number, contents, or order of the<br />

arguments without having to modify the function. They can also be useful<br />

when you have a number of functions that need similar information.<br />

12-17

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

Saved successfully!

Ooh no, something went wrong!