主要内容

Create Function Handle

You can create function handles to named and anonymous functions. You can store multiple function handles in an array, and save and load them, as you would any other variable.

什么是函数句柄?

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:

  • 将功能传递到另一个功能(通常称为功能功能). For example, passing a function to integration and optimization functions, such asintegralfzero.

  • 指定回调函数(例如,响应UI事件或与数据采集硬件进行交互的回调)。

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

  • 从主函数外部调用本地功能。

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

创建功能句柄

To create a handle for a function, precede the function name with an@sign. For example, if you have a function calledmyfunction,创建一个名为f如下:

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:

功能y = 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

没有括号,分配会创建另一个函数句柄。

a = h
a = @ONES

功能处理是您可以传递到其他功能的变量。例如,计算x2on 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.

  • 优先级 - 当具有相同名称的多个函数时,MATLAB使用相同的优先规则来定义功能手柄与调用函数一样。有关更多信息,请参阅Function Precedence Order.

  • 超载 - 当一个或多个参数调用函数句柄时,MATLAB确定了主要参数。如果主要参数是对象,则MATLAB确定对象类是否具有与函数句柄相关函数相同名称过载的方法。如果是这样,则调用对象的方法而不是关联的函数。

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,以及匿名函数的输入参数的逗号分隔列表,arglist. The syntax is:

h = @(arglist)anonymous_function

For example, create a handle,SQR,到一个计算数字正方形的匿名函数,并使用其句柄调用匿名函数。

sqr = @(n)n。^2;X = SQR(3)
x = 9

有关更多信息,请参阅Anonymous 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

保存和加载功能处理

You can save and load function handles in MATLAB, as you would any other variable. In other words, use the节省load功能. If you save a function handle, MATLAB saves the absolute path information. You can invoke the function from any location that MATLAB is able to reach, as long as the file for the function still exists at this location. 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