MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

4 M-File Programming 7 Function elsewhere on the path Finally, a function elsewhere on the path is selected. A function in a directory that is toward the beginning of the path string is given higher precedence. Note Because variables have the highest precedence, if you have created a variable of the same name as a function, MATLAB will not be able to run that function until you clear the variable from memory. Multiple Implementation Types There are five file precedence types. MATLAB uses file precedence to select between identically named functions in the same directory. The order of precedence for file types is 1 Built-in file 2 MEX-files 3 MDL (Simulink® model) file 4 P-code file 5 M-file For example, if MATLAB finds a P-code and an M-file version of a method in a class directory, then the P-code version is used. It is, therefore, important to regenerate the P-code version whenever you edit the M-file. QueryingWhichFunctionMATLABWillCall You can determine which function MATLAB will call using the which command. For example, which pie3 matlabroot/toolbox/matlab/specgraph/pie3.m However, if p is a portfolio object, 4-56

Calling Functions which pie3(p) dir_on_your_path/@portfolio/pie3.m % portfolio method The which command determines which version of pie3 MATLAB will call if you passed a portfolio object as the input argument. To see a list of all versions of a particular function that are on your MATLAB path, use the -all option. See the which reference page for more information on this command. MATLAB Calling Syntax This section explains how to use the MATLAB command and function syntax: • “MATLAB Command Syntax” on page 4-57 • “MATLAB Function Syntax” on page 4-58 • “Passing Arguments with Command and Function Syntax” on page 4-58 • “How MATLAB Recognizes Function Calls That Use Command Syntax” on page 4-60 You can call function M-files from either the MATLAB command line or from within other M-files. Be sure to include all necessary arguments, enclosing input arguments in parentheses and output arguments in square brackets. Note Function names are sensitive to case. When you call a function, use the correct combination of upper and lowercase letters so that the name is an exact match. Otherwise, you risk calling a different function that does matchbutiselsewhereonthepath. You often have the choice of using one of two syntaxes for a function call. You can use either a command or a function type of syntax. This is referred to in MATLAB as command/function duality. MATLAB Command Syntax A function call made in command syntax consists of the function name followed by one or more arguments separated by spaces: functionname in1 in2 ... inN 4-57

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

7 Function elsewhere on the path<br />

Finally, a function elsewhere on the path is selected. A function in a<br />

directory that is toward the beginning of the path string is given higher<br />

precedence.<br />

Note Because variables have the highest precedence, if you have created a<br />

variable of the same name as a function, <strong>MATLAB</strong> will not be able to run that<br />

function until you clear the variable from memory.<br />

Multiple Implementation Types<br />

There are five file precedence types. <strong>MATLAB</strong> uses file precedence to select<br />

between identically named functions in the same directory. The order of<br />

precedence for file types is<br />

1 Built-in file<br />

2 MEX-files<br />

3 MDL (Simulink® model) file<br />

4 P-code file<br />

5 M-file<br />

For example, if <strong>MATLAB</strong> finds a P-code and an M-file version of a method in a<br />

class directory, then the P-code version is used. It is, therefore, important to<br />

regenerate the P-code version whenever you edit the M-file.<br />

QueryingWhichFunction<strong>MATLAB</strong>WillCall<br />

You can determine which function <strong>MATLAB</strong> will call using the which<br />

command. For example,<br />

which pie3<br />

matlabroot/toolbox/matlab/specgraph/pie3.m<br />

However, if p is a portfolio object,<br />

4-56

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

Saved successfully!

Ooh no, something went wrong!