23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2 Data Types<br />

Function Handles<br />

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

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

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

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

Read more about function handles in the section, “Function Handles” on page<br />

4-23.<br />

This section covers the following topics:<br />

• “Constructing and Invoking a Function Handle” on page 2-116<br />

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

• “Simple Function Handle Example” on page 2-117<br />

Constructing and Invoking a Function Handle<br />

You construct a handle for a specific function by preceding the function name<br />

with an @ sign. Use only the function name (withnopathinformation)after<br />

the @ sign:<br />

fhandle = @functionname<br />

Handles to Anonymous Functions<br />

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

function. For example,<br />

sqr = @(x) x.^2;<br />

creates an anonymous function that computes the square of its input<br />

argument x. The variable sqr contains a handle to the anonymous function.<br />

See “Anonymous Functions” on page 5-3 for more information.<br />

Calling a Function Using Its Handle<br />

To execute a function associated with a function handle, use the syntax shown<br />

here, treating the function handle fhandle as if it were a function name:<br />

2-116

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

Saved successfully!

Ooh no, something went wrong!