Documentation

func2str

Construct string from function handle

Syntax

Description

example

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

Examples

collapse all

Convert Function Handle to String

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

fh = @cos;
str = func2str(fh)
str =

cos

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

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

Display Function Handle String Programmatically

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.


% Copyright 2015 The MathWorks, Inc.

function evaluateHandle(fh,x)

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

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

end

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

fh = @sin;
x = pi/2;
evaluateHandle(fh,x)
For input value: 
    1.5708

The function sin evaluates to:
     1

Use a function handle to evaluate $x^2 + 7$ for the specified matrix, A.

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

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

Input Arguments

collapse all

fh — Handle to convert to stringfunction handle

Handle to convert to a string, specified as a function handle.

More About

expand all

Tips

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

See Also

|

Introduced before R2006a

Was this topic helpful?