MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 338 and 339: 4 M-File Programming Improving Perf
- Page 340 and 341: 4 M-File Programming Working with M
- Page 342 and 343: 4 M-File Programming Function Defin
- Page 344 and 345: 4 M-File Programming This is the fi
- Page 346 and 347: 4 M-File Programming The process lo
- Page 348 and 349: 4 M-File Programming Providing Help
- Page 350 and 351: 4 M-File Programming M-File Scripts
- Page 352 and 353: 4 M-File Programming Simple Functio
- Page 354 and 355: 4 M-File Programming 3 Type inmem t
- Page 356 and 357: 4 M-File Programming sqr = @(x) x.^
- Page 358 and 359: 4 M-File Programming Call plotFhand
- Page 360 and 361: 4 M-File Programming This false res
- Page 362 and 363: 4 M-File Programming Create two equ
- Page 364 and 365: 4 M-File Programming • Overloadin
- Page 366 and 367: 4 M-File Programming Here is a more
- Page 368 and 369: 4 M-File Programming axis([xmin fix
- Page 370 and 371: 4 M-File Programming This section c
- Page 372 and 373: 4 M-File Programming addOptional(na
- Page 374 and 375: 4 M-File Programming % Display the
- Page 376 and 377: 4 M-File Programming Arguments That
- Page 378 and 379: 4 M-File Programming Enabling Case-
- Page 380 and 381: 4 M-File Programming Property Funct
- Page 382 and 383: 4 M-File Programming Using nargin a
- Page 384 and 385: 4 M-File Programming 4-52
- Page 386 and 387: 4 M-File Programming Determining Wh
- Page 390 and 391: 4 M-File Programming While the comm
- Page 392 and 393: 4 M-File Programming strcmp str1 st
- Page 394 and 395: 4 M-File Programming On the other h
- Page 396 and 397: 4 M-File Programming City Temp. Hea
- Page 398 and 399: 4 M-File Programming function [vout
- Page 400 and 401: 4 M-File Programming Running Extern
- Page 402 and 403: 5 Types of Functions Overview of MA
- Page 404 and 405: 5 Types of Functions Note Function
- Page 406 and 407: 5 Types of Functions Arrays of Anon
- Page 408 and 409: 5 Types of Functions Display the da
- Page 410 and 411: 5 Types of Functions If you change
- Page 412 and 413: 5 Types of Functions • “Example
- Page 414 and 415: 5 Types of Functions Supply the val
- Page 416 and 417: 5 Types of Functions Nested Functio
- Page 418 and 419: 5 Types of Functions • From a fun
- Page 420 and 421: 5 Types of Functions not shared bet
- Page 422 and 423: 5 Types of Functions You can now us
- Page 424 and 425: 5 Types of Functions Using a Subfun
- Page 426 and 427: 5 Types of Functions count = 125 22
- Page 428 and 429: 5 Types of Functions shape. The mak
- Page 430 and 431: 5 Types of Functions % FILTFCN. Eac
- Page 432 and 433: 5 Types of Functions 0.6667 filt2(1
- Page 434 and 435: 5 Types of Functions unless you dec
- Page 436 and 437: 5 Types of Functions Accessing Help
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