MATLAB Function Reference Previous page   Next Page

Construct function name string from function handle



func2str(fhandle) constructs a string s that holds the name of the function to which the function handle fhandle belongs.

When you need to perform a string operation, such as compare or display, on a function handle, you can use func2str to construct a string bearing the function name.

The func2str command does not operate on nonscalar function handles. Passing a nonscalar function handle to func2str results in an error.


Example 1

Convert a sin function handle to a string:

Example 2

The catcherr function shown here accepts function handle and data arguments and attempts to evaluate the function through its handle. If the function fails to execute, catcherr uses sprintf to display an error message giving the name of the failing function. The function name must be a string for sprintf to display it. The code derives the function name from the function handle using func2str:

The first call to catcherr passes a handle to the round function and a valid data argument. This call succeeds and returns the expected answer. The second call passes the same function handle and an improper data type (a MATLAB structure). This time, round fails, causing catcherr to display an error message that includes the failing function name:

See Also

function_handle, str2func, functions

Previous page  fullfile function Next page

© 1994-2005 The MathWorks, Inc.