라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

function_handle (@)

Handle used in calling functions indirectly


handle = @functionname
handle = @(arglist)anonymous_function


handle = @functionname returns a handle to the specified MATLAB® function.

A function handle is a MATLAB value that provides a means of calling a function indirectly. You can pass function handles in calls to other functions (often called function functions). You can also store function handles in data structures for later use (for example, as Handle Graphics callbacks). A function handle is one of the standard MATLAB data types.

At the time you create a function handle, the function you specify must be on the MATLAB path and in the current scope of the code creating the handle. For example, you can create a handle to a local function as long as you do so from within the file that defines that local function. This condition does not apply when you evaluate the function handle. You can, for example, execute a local function from a separate (out-of-scope) file using a function handle. This requires that the handle was created by the local function (in-scope).

handle = @(arglist)anonymous_function constructs an anonymous function and returns a handle to that function. The body of the function, to the right of the parentheses, is a single MATLAB statement or command. arglist is a comma-separated list of input arguments. Execute the function by calling it by means of the function handle, handle.


Example 1 — Constructing a Handle to a Named Function

The following example creates a function handle for the humps function and assigns it to the variable fhandle.

fhandle = @humps;

Pass the handle to another function in the same way you would pass any argument. This example passes the function handle just created to fminbnd, which then minimizes over the interval [0.3, 1].

x = fminbnd(fhandle, 0.3, 1)
x =

The fminbnd function evaluates the @humps function handle. A small portion of the fminbnd file is shown below. In line 1, the funfcn input parameter receives the function handle @humps that was passed in. The statement, in line 113, evaluates the handle.

1    function [xf,fval,exitflag,output] = ...
113  fx = funfcn(x,varargin{:});

Example 2 — Constructing a Handle to an Anonymous Function

The statement below creates an anonymous function that finds the square of a number. When you call this function, MATLAB assigns the value you pass in to variable x, and then uses x in the equation x.^2:

sqr = @(x) x.^2;

The @ operator constructs a function handle for this function, and assigns the handle to the output variable sqr. As with any function handle, you execute the function associated with it by specifying the variable that contains the handle, followed by a comma-separated argument list in parentheses. The syntax is

fhandle(arg1, arg2, ..., argN)

To execute the sqr function defined above, type

a = sqr(5)
a =

Because sqr is a function handle, you can pass it in an argument list to other functions. The code shown here passes the sqr anonymous function to the MATLAB integral function to compute its integral from zero to one:

integral(sqr, 0, 1)
ans =

Example 3 — Using an Array of Function Handles

This example creates a structure array of function handles S and then applies each handle in the array to the output of a linspace calculation in one operation using structfun:

S.a = @sin;  S.b = @cos;  S.c = @tan;
structfun(@(x)x(linspace(1,4,3)), S, 'UniformOutput', false)
ans = 
    a: [0.8415 0.5985 -0.7568]
    b: [0.5403 -0.8011 -0.6536]
    c: [1.5574 -0.7470 1.1578]

More About

collapse all


The function handle is a standard MATLAB data type. As such, you can manipulate and operate on function handles in the same manner as on other MATLAB data types. This includes using function handles in structures and cell arrays:

S.a = @sin;  S.b = @cos;  S.c = @tan;
C = {@sin, @cos, @tan};

However, standard matrices or arrays of function handles are not supported:

A = [@sin, @cos, @tan];        % This is not supported

For nonoverloaded functions, local functions, and private functions, a function handle references just the one function specified in the @functionname syntax. When you evaluate an overloaded function by means of its handle, the arguments the handle is evaluated with determine the actual function that MATLAB dispatches to.

Use isa(h, 'function_handle') to see if variable h is a function handle.

Introduced before R2006a

Was this topic helpful?