functions - Information about function handle - MATLAB (original) (raw)

Main Content

Information about function handle

Syntax

Description

[s](#f71-813404-s) = functions([fh](#f71-813404-fh)) returns information about a function handle. This information includes the function name, type, and file name.

Use the functions function for querying and debugging purposes only.

Note

Do not use functions programmatically because its behavior could change in subsequent MATLAB® releases.

example

Examples

collapse all

Display Information About Named Function Handle

Create a function handle and display its information.

fh = @cos; s = functions(fh)

s = struct with fields: function: 'cos' type: 'simple' file: ''

Display Information About Anonymous Function Handle

Create a function handle to an anonymous function. Display its information and values of required variables.

Create a handle to the function x 2 + y, and invoke the function using the handle.

y = 7; fh = @(x)x.^2+y; z = fh(2)

Display information about the function handle.

s =

        function: '@(x)x.^2+y'
            type: 'anonymous'
            file: ''
       workspace: {[1x1 struct]}
within_file_path: '__base_function'

The function handle contains the required value of y.

Display Information About Nested and Local Function Handle

Create a function that returns handles to local and nested functions. Display their information.

Create the following function in a file, functionsExample.m, in your working folder. The function returns handles to a nested and local function.

function [hNest,hLocal] = functionsExample(v)

hNest = @nestFunction; hLocal = @localFunction;

function y = nestFunction(x)
    y = x + v;
end

end

function y = localFunction(z) y = z + 1; end

Invoke the function.

[hNest,hLocal] = functionsExample(13)

hNest =

@functionsExample/nestFunction

hLocal =

@localFunction

Display information about the handle to the nested function.

s1 =

 function: 'functionsExample/nestFunction'
     type: 'nested'
     file: 'C:\work\functionsExample.m'
workspace: {[1x1 struct]}

Display information about the handle to the local function.

s2 =

 function: 'localFunction'
     type: 'scopedfunction'
     file: 'C:\work\functionsExample.m'
parentage: {'localFunction'  'functionsExample'}

Input Arguments

collapse all

fh — Handle to query

function handle

Handle to query, specified as a function handle.

Output Arguments

collapse all

s — Information about function handle

structure

Information about a function handle, returned as a structure. The structure contains the following fields.

Field Name Field Description
function Function name. If the function associated with the handle is a nested function, the function name takes the form main_function/nested_function.
type Function type. For example 'simple', 'nested', 'scopedfunction', or 'anonymous'.
file Full path to the function with the file extension. If the function is a local or nested function, then file is the full path to the main function. If the function is a built-in MATLAB function, thenfile is an empty character array ('').If the function is an anonymous function and defined in the command line or in a file not on the MATLAB path, then file is an empty character array ('').If the function is an anonymous function and defined in a file on the MATLAB path, then file is the full path to the file.If you load a saved function handle, then file is an empty character array ('').If the function is a class method, thenfile is an empty character array ('').

The structure has additional fields depending on the type of function associated with the handle. For example, a local function has a parentage field, and an anonymous function has a workspace field. Use the information in s for querying and debugging purposes only.

Extended Capabilities

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced before R2006a