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 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

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

Simple Function Example<br />

The average functionisasimpleM-filethatcalculatestheaverageofthe<br />

elements in a vector:<br />

function y = average(x)<br />

% AVERAGE Mean of vector elements.<br />

% AVERAGE(X), where X is a vector, is the mean of vector<br />

% elements. Nonvector input results in an error.<br />

[m,n] = size(x);<br />

if (~((m == 1) | (n == 1)) | (m == 1 & n == 1))<br />

error('Input must be a vector')<br />

end<br />

y = sum(x)/length(x); % Actual computation<br />

Try entering these commands in an M-file called average.m. Theaverage<br />

function accepts a single input argument and returns a single output<br />

argument. To call the average function, enter<br />

z = 1:99;<br />

average(z)<br />

ans =<br />

50<br />

Types of Functions<br />

<strong>MATLAB</strong> provides the following types of functions. Each function type is<br />

described in more detail in a later section of this documentation:<br />

• The “Primary M-File Functions” on page 5-15 is the first function in an<br />

M-file and typically contains the main program.<br />

• “Subfunctions” on page 5-33 act as subroutines to the main function. You<br />

can also use them to define multiple functions within a single M-file.<br />

• “Nested Functions” on page 5-16 are functions defined within another<br />

function. They can help to improve the readability of your program and<br />

also give you more flexible access to variables in the M-file.<br />

• “Anonymous Functions” on page 5-3 provide a quick way of making a<br />

function from any <strong>MATLAB</strong> expression. You can compose anonymous<br />

4-20

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

Saved successfully!

Ooh no, something went wrong!