This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Construct character vector from function handle


c = func2str(fh)



c = func2str(fh) constructs a character vector, c, that contains the name of the function associated with the function handle, fh. If fh is associated with an anonymous function, func2str returns a character vector that represents the anonymous function.


collapse all

Create function handles for both the cos function and for an anonymous function, and then convert them to character vectors.

fh = @cos;
c = func2str(fh)
c = 
fh = @(x,y)sqrt(x.^2+y.^2);
c = func2str(fh);

disp(['Anonymous function: ' c])
Anonymous function: @(x,y)sqrt(x.^2+y.^2)

Create a function that evaluates a function handle for a single input.

Create the following function in a file, evaluateHandle.m, in your working folder.

function evaluateHandle(fh,x)

y = fh(x);
str = func2str(fh);

disp('For input value: ')
disp(['The function ' str ' evaluates to:'])


Use a function handle to evaluate the sin function at pi/2.

fh = @sin;
x = pi/2;
For input value: 

The function sin evaluates to:

Use a function handle to evaluate for the specified matrix, A.

fh = @(x) x.^2+7;
A = [1 2;0 1];
For input value: 
     1     2
     0     1

The function @(x)x.^2+7 evaluates to:
     8    11
     7     8

Input Arguments

collapse all

Handle to convert to a character vector, specified as a function handle.


  • You lose variables stored in the function handle when you convert it to a character vector using func2str, and then back to a handle using str2func.

Extended Capabilities

Introduced before R2006a