| Programming and Data Types | ![]() |
Types of Function Handles
The information returned by functions varies depending on the type of function represented by the function handle. This section explains what is returned for each type of function. The categories of function handles are:
These are handles to nonoverloaded MATLAB built-in or M-file functions. Any function handles for which the function type has not yet been determined (e.g., Java methods, nonexistent functions), also fall into this category.
function: function name type: 'simple' file: 'MATLAB built-in function' for built-ins path and name of the default M-file for nonbuilt-ins (there is no methods field)
Using functions on a function handle to a built-in function
Using functions on a function handle to a nonbuilt-in function
functions(@fzero) ans = function: 'fzero' type: 'simple' file: 'matlabroot\toolbox\matlab\funfun\fzero.m'
These are handles to MATLAB built-in or M-file functions that are overloaded implementations for different classes.
function: function name type: 'overloaded' file: 'MATLAB built-in function' for built-ins path and name of the default M-file for nonbuilt-ins methods: [1x1 struct]
Using functions on a function handle to a built-in function
functions(@display) ans = function: 'display' type: 'overloaded' file: 'MATLAB built-in function' methods: [1x1 struct]
Using functions on a function handle to a nonbuilt-in function
functions(@deblank) ans = function: 'deblank' type: 'overloaded' file: 'matlabroot\toolbox\matlab\strfun\deblank.m' methods: [1x1 struct]
These are handles to functions that construct objects of MATLAB classes.
function: function name type: 'constructor' file: path and name of the constructor M-file (there is no methods field)
Using functions on a function handle to a constructor function
functions(@inline) ans = function: 'inline' type: 'constructor' file: 'matlabroot\toolbox\matlab\funfun\@inline\inline.m'
These are handles to MATLAB subfunctions, which are functions defined within an M-file that are only visible to the primary function of that M-file. When you use functions on a subfunction handle, the file field of the return structure contains the path and name of the M-file in which the subfunction is defined.
function: function name type: 'subfunction' file: path and name of the M-file defining the subfunction (there is no methods field)
The getLocalHandle M-file, shown below, defines a primary function and a subfunction, named subfunc.
% -- File GETLOCALHANDLE.M -- function subhandle = getLocalHandle() subhandle = @subfunc; % return handle to subfunction function subfunc() disp 'running subfunc'
A call to getLocalHandle returns a function handle to the subfunction. When you pass that handle to functions, it returns the following information.
fhandle = getLocalHandle; functions(fhandle) ans = function: 'subfunc' type: 'subfunction' file: '\home\user4\getLocalHandle.m'
These are handles to MATLAB private functions, which are functions defined in a private subdirectory that are only visible to functions in the parent directory. When you use functions on a private function handle, the file field of the return structure contains the path and name of the M-file in the private subdirectory that defines the function.
function: function name type: 'private' file: path and name of the M-file in \private (there is no methods field)
The getPrivateHandle function, shown below, returns a handle to a private function named privatefunc.
% -- File GETPRIVATEHANDLE.M -- function privhandle = getPrivateHandle() privhandle = @privatefunc; % return handle to private function
The following function, privatefunc, resides in the \private subdirectory.
A call to getPrivateHandle returns a handle to the function, privatefunc, defined in \private. When you pass that handle to functions, it returns the following information.
fhandle = getPrivateHandle; functions(fhandle) ans = function: 'privatefunc' type: 'private' file: '\home\user4\private\privatefunc.m'
| Fields Returned by the Functions Command | Function Handle Operations | ![]() |