MATLAB Programming
MATLAB Programming MATLAB Programming
4 M-File Programming Simple Function Example The average functionisasimpleM-filethatcalculatestheaverageofthe elements in a vector: function y = average(x) % AVERAGE Mean of vector elements. % AVERAGE(X), where X is a vector, is the mean of vector % elements. Nonvector input results in an error. [m,n] = size(x); if (~((m == 1) | (n == 1)) | (m == 1 & n == 1)) error('Input must be a vector') end y = sum(x)/length(x); % Actual computation Try entering these commands in an M-file called average.m. Theaverage function accepts a single input argument and returns a single output argument. To call the average function, enter z = 1:99; average(z) ans = 50 Types of Functions MATLAB provides the following types of functions. Each function type is described in more detail in a later section of this documentation: • The “Primary M-File Functions” on page 5-15 is the first function in an M-file and typically contains the main program. • “Subfunctions” on page 5-33 act as subroutines to the main function. You can also use them to define multiple functions within a single M-file. • “Nested Functions” on page 5-16 are functions defined within another function. They can help to improve the readability of your program and also give you more flexible access to variables in the M-file. • “Anonymous Functions” on page 5-3 provide a quick way of making a function from any MATLAB expression. You can compose anonymous 4-20
M-File Scripts and Functions functions either from within another function or at the MATLAB command prompt. • “Overloaded Functions” on page 5-37 are useful when you need to create a function that responds to different types of inputs accordingly. They are similar to overloaded functions in any object-oriented language. • “Private Functions” on page 5-35 give you a way to restrict access to a function. You can call them only from an M-file function in the parent directory. You might also see the term function functions in the documentation. This is not really a separate function type. The term function functions refers to any functions that accept another function as an input argument. You can pass a function to another function using a function handle. Identifying Dependencies Most any program you write will make calls to other functions and scripts. If you need to know what other functions and scripts your program is dependent upon, use one of the techniques described below. Simple Display of M-File Dependencies For a simple display of all M-files referenced by a particular function, follow these steps: 1 Type clear functions to clear all functions from memory (see Note below). Note clear functions does not clear functions locked by mlock. Ifyou have locked functions (which you can check using inmem) unlockthemwith munlock, andthenrepeatstep1. 2 Execute the function you want to check. Note that the function arguments you choose to use in this step are important, because you can get different results when calling the same function with different arguments. 4-21
- Page 302 and 303: 3 Basic Program Components 40 Thisi
- Page 304 and 305: 3 Basic Program Components ans = 10
- Page 306 and 307: 3 Basic Program Components X = -pi:
- Page 308 and 309: 3 Basic Program Components Program
- Page 310 and 311: 3 Basic Program Components if A S1
- Page 312 and 313: 3 Basic Program Components end disp
- Page 314 and 315: 3 Basic Program Components while St
- Page 316 and 317: 3 Basic Program Components Program
- Page 318 and 319: 3 Basic Program Components Asterisk
- Page 320 and 321: 3 Basic Program Components Numeric
- Page 322 and 323: 3 Basic Program Components Cell Arr
- Page 324 and 325: 3 Basic Program Components Dynamic
- Page 326 and 327: 3 Basic Program Components The purp
- Page 328 and 329: 3 Basic Program Components Function
- Page 330 and 331: 3 Basic Program Components MATLAB F
- Page 332 and 333: 3 Basic Program Components Forcing
- Page 334 and 335: 4 M-File Programming Function Argum
- Page 336 and 337: 4 M-File Programming Saving the Pro
- 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 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 388 and 389: 4 M-File Programming 7 Function els
- 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
M-File Scripts and Functions<br />
functions either from within another function or at the <strong>MATLAB</strong> command<br />
prompt.<br />
• “Overloaded Functions” on page 5-37 are useful when you need to create a<br />
function that responds to different types of inputs accordingly. They are<br />
similar to overloaded functions in any object-oriented language.<br />
• “Private Functions” on page 5-35 give you a way to restrict access to a<br />
function. You can call them only from an M-file function in the parent<br />
directory.<br />
You might also see the term function functions in the documentation. This is<br />
not really a separate function type. The term function functions refers to any<br />
functions that accept another function as an input argument. You can pass a<br />
function to another function using a function handle.<br />
Identifying Dependencies<br />
Most any program you write will make calls to other functions and scripts. If<br />
you need to know what other functions and scripts your program is dependent<br />
upon, use one of the techniques described below.<br />
Simple Display of M-File Dependencies<br />
For a simple display of all M-files referenced by a particular function, follow<br />
these steps:<br />
1 Type clear functions to clear all functions from memory (see Note below).<br />
Note clear functions does not clear functions locked by mlock. Ifyou<br />
have locked functions (which you can check using inmem) unlockthemwith<br />
munlock, andthenrepeatstep1.<br />
2 Execute the function you want to check. Note that the function arguments<br />
you choose to use in this step are important, because you can get different<br />
results when calling the same function with different arguments.<br />
4-21