func2str - Construct character vector from function handle - MATLAB (original) (raw)
Main Content
Construct character vector from function handle
Syntax
Description
c = func2str([fh](#f71-893093-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.
Examples
Convert Function Handle to Character Vector
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)
fh = @(x,y)sqrt(x.^2+y.^2); c = func2str(fh);
disp(['Anonymous function: ' c])
Anonymous function: @(x,y)sqrt(x.^2+y.^2)
Programmatically Display Function Handle Name as Character Vector
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(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 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
fh
— Handle to convert to character vector
function handle
Handle to convert to a character vector, specified as a function handle.
Tips
- You lose variables stored in the function handle when you convert it to a character vector using
func2str
, and then back to a handle usingstr2func
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- If the input argument is associated with an anonymous function, then the generated code returns the character vector
'@(...)...'
.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Version History
Introduced before R2006a