23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Function Handles<br />

Function Handles<br />

A function handle is a <strong>MATLAB</strong> value that provides a means of calling a<br />

function indirectly. You can pass function handles in calls to other functions<br />

(often called function functions). You can also store function handles in data<br />

structures for later use (for example, as Handle Graphics callbacks).<br />

This section covers the following topics:<br />

• “Constructing a Function Handle” on page 4-23<br />

• “Calling a Function Using Its Handle” on page 4-24<br />

• “Functions That Operate on Function Handles” on page 4-26<br />

• “Comparing Function Handles” on page 4-26<br />

• “Additional Information on Function Handles” on page 4-31<br />

Constructing a Function Handle<br />

Use the following syntax to construct a function handle, preceding the name<br />

of the function with an @ sign. Use only the function name, withnopath<br />

information, after the @ sign:<br />

fhandle = @functionname<br />

<strong>MATLAB</strong>mapsthehandletothefunction you specify and saves this mapping<br />

information in the handle. If there is more than one function with this name,<br />

<strong>MATLAB</strong> maps to the one function source it would dispatch to if you were<br />

actually calling the function.<br />

A function handle retains that same mapping even if its corresponding<br />

function goes out of scope. For example, if, after creating the handle, you<br />

changethe<strong>MATLAB</strong>pathsothatadifferentfunctionofthesamenamenow<br />

takes precedence, invoking the function handle still executes the code to<br />

which the handle was originally mapped.<br />

Handles to Anonymous Functions<br />

Another way to construct a function handle is to create an anonymous<br />

function. For example,<br />

4-23

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

Saved successfully!

Ooh no, something went wrong!