|Programming and Data Types|
A function handle is a MATLAB data type that contains information used in referencing a function. When you create a function handle, MATLAB stores in the handle all the information about the function that it needs to execute, or evaluate, it later on. Typically, a function handle is passed in an argument list to other functions. It is then used in conjunction with
feval to evaluate the function to which the handle belongs.
A MATLAB function handle is more than just a reference to a function. It often represents a collection of function methods, overloaded to handle different argument types. When you create a handle to a function, MATLAB takes a snapshot of all built-in and M-file methods of that name that are on the MATLAB path and in scope at that time, and stores access information for all of those methods in the handle.
When you evaluate a function handle, MATLAB considers only those functions that were stored within the handle when it was created. Other functions that might now be on the path or in scope are not considered. It is the combination of which function methods are mapped to by the handle and what arguments the handle is evaluated with that determines which is the actual function that MATLAB dispatches to.
Benefits of Using Function Handles
Function handles enable you to do all of the following:
This section also includes an example of using a simple function handle. See A Simple Function Handle.
Pass Function Access Information to Other Functions
You can pass a function handle as an argument in a call to another function. The handle contains access information that enables the receiving function to call the function for which the handle was constructed.
You can evaluate a function handle from within another function even if the handle's function is not in the scope of the evaluating function. This is because the function performing the evaluation has all the information it needs within the function handle.
For the same reason, you can also evaluate a function handle even when the handle's function is no longer on the MATLAB search path.
You must use the MATLAB
feval command to evaluate the function in a function handle. When you pass a function handle as an argument into another function, then the function receiving the handle uses
feval to evaluate the function handle.
Capture All Methods of An Overloaded Function
Because many MATLAB functions are overloaded, a function handle often maps to a number of code sources (e.g., built-in code, M-files), that implement the function. A function handle stores the access to all of the overloaded sources, or methods, that are on the MATLAB path at the time the handle is created.
When you evaluate an overloaded function handle, MATLAB follows the usual rules of selecting which method to evaluate, basing the selection on the argument types passed in the function call. See How MATLAB Determines Which Method to Call, for more information on how MATLAB selects overloaded functions.
For example, there are three built-in functions and one M-file function that define the
abs function on the standard MATLAB path. A function handle created for the
abs function contains access information on all four of these function sources. If you evaluate the function with an argument of the
double type, then the built-in function that takes a
double argument is executed.
Allow Wider Access to Subfunctions and Private Functions
By definition, all MATLAB functions have a certain scope. They are visible to other MATLAB entities within that scope, but not visible outside of it. You can invoke a function directly from another function that is within its scope, but not from a function outside that scope.
Subfunctions and private functions are, by design, limited in their visibility to other MATLAB functions. You can invoke a subfunction only by another function that is defined within the same M-file. You can invoke a private function only from a function in the directory immediately above the
When you create a handle to a function that has limited scope, the function handle stores all the information MATLAB needs to evaluate the function from any location in the MATLAB environment. If you create a handle to a subfunction while the subfunction is in scope, (that is, you create it from within the M-file that defines the subfunction), you can then pass the handle to code that resides outside of that M-file and evaluate the subfunction from beyond its usual scope. The similar case holds true for private functions.
Ensure Reliability When Evaluating Functions
Function handles allow you more control over what methods get executed when a function is evaluated. If you create a function handle for a function with overloaded methods, making sure that only the intended methods are within scope when the handle is created gives you control over which methods are executed when MATLAB evaluates the handle. This can isolate you from methods that might be in scope at the time of evaluation that you didn't know about.
Reduce the Number of Files That Define Your Functions
You can use function handles to help reduce the number of M-files required to define your functions. The problem with grouping a number of functions in one M-file is that this defines them as subfunctions, and thus reduces their scope in MATLAB. Using function handles to access these subfunctions removes this limitation. This enables you to group functions as you want and reduce the number of files you have to manage.
Improve Performance in Repeated Operations
MATLAB performs a lookup on a function at the time you create a function handle and then stores this access information in the handle itself. Once defined, you can use this handle in repeated evaluations without incurring the performance delay associated with function lookup each time.
Manipulate Handles in Arrays, Structures, and Cell Arrays
As a standard MATLAB data type, a function handle can be manipulated and operated on in the same manner as other MATLAB data types. You can create arrays, structures, or cell arrays of function handles. Access individual function handles within these data structures in the same way that you access elements of a numeric array or structure.
Create n-dimensional arrays of handles using either of the concatenation methods used to form other types of MATLAB arrays,
cat. All operations involving matrix manipulation are supported for function handles.
|Function Handles||A Simple Function Handle|