Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Creating a Function Handle

You construct a handle for a specific function by preceding the function name with an @ sign. The syntax is:

h = @functionname

where h is the variable to which the returned function handle is assigned.

Use only the function name, with no path information, after the @ sign. If there is more than one function with this name, MATLAB® associates with the handle the one function source it would dispatch to if you were actually calling the function.

Create a handle h for a function plot that is on your MATLAB path:

h = @plot;

Once you create a handle for a function, you can invoke the function by means of the handle instead of using the function name. Because the handle contains the absolute path to its function, you can invoke the function from any location that MATLAB is able to reach, as long as the program file for the function still exists at this location. This means that functions in one file can call functions that are not on the MATLAB path, local functions in a separate file, or even functions that are private to another folder, and thus not normally accessible to that caller.

Maximum Length of a Function Name

Function names used in handles are unique up to N characters, where N is the number returned by the function namelengthmax. If the function name exceeds that length, MATLAB truncates the latter part of the name.

For function handles created for Java® constructors, the length of any segment of the package name or class name must not exceed namelengthmax characters. (The term segment refers to any portion of the name that lies before, between, or after a dot. For example, java.lang.String has three segments). The overall length of the string specifying the package and class has no limit.

The Role of Scope, Precedence, and Overloading When Creating a Function Handle

At the time you create a function handle, MATLAB must decide exactly which function it is to associate the handle to. In doing so, MATLAB uses the same rules used to determine which file to invoke when you make a function call. To make this determination, MATLAB considers the following:

  • Scope — The function named must be on the MATLAB path at the time the handle is constructed.

  • Precedence — MATLAB selects which function(s) to associate the handle with, according to the function precedence rules described under Determining Which Function Gets Called.

  • Overloading — If additional files on the path overload the function for any of the fundamental MATLAB classes, such as double or char, then MATLAB associates the handle with these files, as well.

Program files that overload a function for classes other than the standard MATLAB classes are not associated with the function handle at the time it is constructed. Function handles do operate on these types of overloaded functions, but MATLAB determines which implementation to call at the time of evaluation in this case.

Obtaining Permissions from Class Methods

When creating a function handle inside a method of a class, the function is resolved using the permissions of that method. When MATLAB invokes the function handle, it does so using the permissions of the class. This gives MATLAB the same access as the location where the function handle was created, including access to private and protected methods accessible to that class.

Example

This example defines two methods. One, updateObj, defines a listener for an event called Update, and the other , callbackfcn, responds to this event whenever it should occur. The latter function is a private function and thus would not normally be within the scope of the notify function. However, because @callbackfcn is actually a function handle, it retains the permissions of the context that created the function handle:

classdef updateObj < handle
   events
      Update
   end
   
   methods
      function obj = updateObj(varargin)
         addlistener(obj, 'Update', @callbackfcn);
         notify(obj, 'Update');
      end
   end
   
   methods (Access = private)
      function obj = callbackfcn(obj, varargin)
         disp('Object Updated')
         disp(obj);
      end
   end
end

To run this function, invoke updateObj at the MATLAB command line.

Using Function Handles for Anonymous Functions

Function handles also serve as the means of invoking anonymous functions. An anonymous function is a one-line expression-based MATLAB function that does not require a program file.

For example, the statement

sqr = @(x) x.^2;

creates an anonymous function that computes the square of its input argument x. The @ operator makes sqr a function handle, giving you a means of calling the function:

sqr(20)
ans =
   400

Like nested functions, a handle to an anonymous function also stores all data that will be needed to resolve the handle when calling the function. Shares same issues as nested functions do.

See the documentation on Anonymous Functions for more information.

Arrays of Function Handles

To create an array of function handles, you must use a cell array:

trigFun = {@sin, @cos, @tan};

For example, to plot the cosine of the range -pi to pi at 0.01 intervals, use

plot(trigFun{2}(-pi:0.01:pi))
Was this topic helpful?