Handle to function
A function handle is a MATLAB® data type that represents a function. A typical use of function handles is to pass a function to another function. For example, you can use function handles as input arguments to functions that evaluate mathematical expressions over a range of values. Other typical uses of function handles include:
Specifying callback functions (for example, a callback that responds to a UI event or interacts with data acquisition hardware).
Constructing handles to functions defined inline instead of stored in a program file (anonymous functions).
Create a function handle using the
@ operator. Function handles can
represent either named or anonymous functions.
Named function handles represent functions in existing program files, including functions that are part of MATLAB and functions that you create using the
functionkeyword. To create a handle to a named function, precede the function name with
For example, create a handle to the
sinfunction, and then use
fminbndto find the value of x that minimizes sin(x) in the range from 0 to :
f = @sin; m = fminbnd(f,0,2*pi);
Anonymous function handles (often called anonymous functions) represent single inline executable expressions that return one output. To define an anonymous function, enclose input argument names in parentheses immediately after the
@operator, and then specify the executable expression.
For example, create a handle to an anonymous function that evaluates the expression x2 − y2:
f = @(x,y) (x.^2 - y.^2);
Anonymous functions can accept multiple inputs but return only one output.
Find Integral of Named Function
In a file in your current folder, create a function named
cubicPoly that accepts an input to evaluate the cubic polynomial .
function y = cubicPoly(x) y = x.^3 + x.^2 + x + 1; end
To find the integral of
1, pass a handle to the
cubicPoly function to
q = integral(@cubicPoly,0,1)
q = 2.0833
Find Integral of Anonymous Function
Create the handle
f to an anonymous function that evaluates the cubic polynomial for a given value of .
f = @(x) x.^3 + x.^2 + x + 1;
To find the integral of the anonymous function from
1, pass its handle to
q = integral(f,0,1)
q = 2.0833
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This datatype fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version HistoryIntroduced before R2006a
R2023a: Improved performance when invoking handles to named functions
Invoking handles to named functions that are not nested shows improved performance.
Invoking such function handles no longer results in an overhead compared to calling
functions directly. For example, in a file named
myFun.m in your current
folder, create the
function y = myFun(x) y = x; end
In a file named
timingTest.m in your current folder, create a
function that invokes a handle to
timingTest function is about 40x faster than in the previous
function t = timingTest f = @myFun; n = 1e7; tic for i = 1:n out = f(3); end t = toc; end
The approximate execution times are:
R2022b: 4.4 s
R2023a: 0.11 s
The code was timed on a Windows® 10, Intel®
Xeon® CPU E5-1650 v4 @ 3.60 GHz test system by calling the