Documentation

Create Function Handle

您可以创建命名和anony函数处理mous functions. You can store multiple function handles in an array, and save and load them, as you would any other variable.

What Is a Function Handle?

A function handle is a MATLAB®data type that stores an association to a function. Indirectly calling a function enables you to invoke the function regardless of where you call it from. Typical uses of function handles include:

  • Pass a function to another function (often calledfunction functions). For example, passing a function to integration and optimization functions, such asintegralandfzero.

  • Specify callback functions. For example, a callback that responds to a UI event or interacts with data acquisition hardware.

  • Construct handles to functions defined inline instead of stored in a program file (anonymous functions).

  • Call local functions from outside the main function.

You can see if a variable,h, is a function handle usingisa(h,'function_handle').

Creating Function Handles

To create a handle for a function, precede the function name with an@sign. For example, if you have a function calledmyfunction, create a handle namedfas follows:

f = @myfunction;

You call a function using a handle the same way you call the function directly. For example, suppose that you have a function namedcomputeSquare, defined as:

functiony = computeSquare(x) y = x.^2;end

Create a handle and call the function to compute the square of four.

f = @computeSquare; a = 4; b = f(a)
b = 16

If the function does not require any inputs, then you can call the function with empty parentheses, such as

h = @ones; a = h()
a = 1

Without the parentheses, the assignment creates another function handle.

a = h
a = @ones

Function handles are variables that you can pass to other functions. For example, calculate the integral ofx2on the range [0,1].

q = integral(f,0,1);

Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location. You do not have to specify the path to the function when creating the handle, only the function name.

Keep the following in mind when creating handles to functions:

  • Name length — Each part of the function name (including package and class names) must be less than the number specified bynamelengthmax. Otherwise, MATLAB truncates the latter part of the name.

  • Scope — The function must be in scope at the time you create the handle. Therefore, the function must be on the MATLAB path or in the current folder. Or, for handles to local or nested functions, the function must be in the current file.

  • Precedence — When there are multiple functions with the same name, MATLAB uses the same precedence rules to define function handles as it does to call functions. For more information, seeFunction Precedence Order.

  • Overloading — If the function you specify overloads a function in a class that is not a fundamental MATLAB class, the function is not associated with the function handle at the time it is constructed. Instead, MATLAB considers the input arguments and determines which implementation to call at the time of evaluation.

Anonymous Functions

You can create handles to anonymous functions. An anonymous function is a one-line expression-based MATLAB function that does not require a program file. Construct a handle to an anonymous function by defining the body of the function,anonymous_function, and a comma-separated list of input arguments to the anonymous function,arglist. The syntax is:

h = @(arglist)anonymous_function

For example, create a handle,sqr, to an anonymous function that computes the square of a number, and call the anonymous function using its handle.

sqr = @(n) n.^2; x = sqr(3)
x = 9

For more information, seeAnonymous Functions.

Arrays of Function Handles

You can create an array of function handles by collecting them into a cell or structure array. For example, use a cell array:

C = {@sin, @cos, @tan}; C{2}(pi)
ans = -1

Or use a structure array:

S.a = @sin; S.b = @cos; S.c = @tan; S.a(pi/2)
ans = 1

Saving and Loading Function Handles

You can save and load function handles in MATLAB, as you would any other variable. In other words, use thesaveandload功能. If you save a function handle, MATLAB does not save the path information. If you load a function handle, and the function file no longer exists on the path, the handle is invalid. An invalid handle occurs if the file location or file name has changed since you created the handle. If a handle is invalid, MATLAB might display a warning when you load the file. When you invoke an invalid handle, MATLAB issues an error.

See Also

|||

Related Examples

More About