MATLAB Programming

MATLAB Programming MATLAB Programming

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

5 Types of Functions Overview of MATLAB Function Types There are essentially two ways to create a new function in MATLAB: in a command entered at run-time, or in a file saved to permanent storage. The command-oriented function, called an anonymous function, isrelatively brief in its content. It consists of a single MATLAB statement that can interact with multiple input and output arguments. The benefit of using anonymous functions is that you do not have to edit and maintain a file for functions that require only a brief definition. There are several types of functions that are stored in files (called M-files). The most basic of these are primary functions and subfunctions. Primary functions are visible to other functions outside of their M-file, while subfunctions, generally speaking, are not. That is, you can call a primary function from an anonymous function or from a function defined in a separate M-file, whereas you can call a subfunction only from functions within the same M-file. (See the Description section of the function_handle reference page for information on making a subfunction externally visible.) Two specific types of primary M-file functions are the private and overloaded function. Private functions are visible only to a limited group of other functions. This type of function can be useful if you want to limit access to a function, or when you choose not to expose the implementation of a function. Overloaded functions act the same way as overloaded functions in most computer languages. You can create multiple implementations of a function so that each responds accordingly to different types of inputs. ThelasttypeofMATLABfunctionisthenested function. Nested functions are not an independent function type; they exist within the body of one of the other types of functions discussed here (with the exception of anonymous functions), and also within other nested functions. One type of function that is not discussed in this chapter is the MATLAB built-in function. Built-ins are defined only as executables internal to MATLAB. See “Built-In Functions” on page 3-111 for more information. 5-2

Anonymous Functions Anonymous Functions Anonymous functions give you a quick means of creating simple functions without having to create M-files each time. You can construct an anonymous function either at the MATLAB command line or in any M-file function or script. This section covers • “Constructing an Anonymous Function” on page 5-3 • “Arrays of Anonymous Functions” on page 5-6 • “Outputs from Anonymous Functions” on page 5-7 • “Variables Used in the Expression” on page 5-8 • “Examples of Anonymous Functions” on page 5-11 Constructing an Anonymous Function The syntax for creating an anonymous function from an expression is fhandle = @(arglist) expr Starting from the right of this syntax statement, the term expr represents the body of the function: the code that performs the main task your function is to accomplish. This consists of any single, valid MATLAB expression. Next is arglist, which is a comma-separated list of input arguments to be passed to the function. These two components are similar to the body and argument list components of any function. Leading off the entire right side of this statement is an @ sign. The @ sign is the MATLAB operator that constructs a function handle. Creating a function handle for an anonymous function gives you a means of invoking the function. It is also useful when you want to pass your anonymous function in a call to some other function. The @ sign is a required part of an anonymous function definition. 5-3

Anonymous Functions<br />

Anonymous Functions<br />

Anonymous functions give you a quick means of creating simple functions<br />

without having to create M-files each time. You can construct an anonymous<br />

function either at the <strong>MATLAB</strong> command line or in any M-file function or<br />

script.<br />

This section covers<br />

• “Constructing an Anonymous Function” on page 5-3<br />

• “Arrays of Anonymous Functions” on page 5-6<br />

• “Outputs from Anonymous Functions” on page 5-7<br />

• “Variables Used in the Expression” on page 5-8<br />

• “Examples of Anonymous Functions” on page 5-11<br />

Constructing an Anonymous Function<br />

The syntax for creating an anonymous function from an expression is<br />

fhandle = @(arglist) expr<br />

Starting from the right of this syntax statement, the term expr represents the<br />

body of the function: the code that performs the main task your function is to<br />

accomplish. This consists of any single, valid <strong>MATLAB</strong> expression. Next is<br />

arglist, which is a comma-separated list of input arguments to be passed to<br />

the function. These two components are similar to the body and argument list<br />

components of any function.<br />

Leading off the entire right side of this statement is an @ sign. The @ sign is<br />

the <strong>MATLAB</strong> operator that constructs a function handle. Creating a function<br />

handle for an anonymous function gives you a means of invoking the function.<br />

It is also useful when you want to pass your anonymous function in a call to<br />

some other function. The @ sign is a required part of an anonymous function<br />

definition.<br />

5-3

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

Saved successfully!

Ooh no, something went wrong!