Documentation |
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.
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 = 0.6370
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] = ... fminbnd(funfcn,ax,bx,options,varargin) . . . 113 fx = funfcn(x,varargin{:});
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 = 25
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 = 0.3333
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]