| Programming and Data Types | ![]() |
Handling Error Conditions
The following are error conditions associated with the use of function handles.
Handles to Nonexistent Functions
If you create a handle to a function that does not exist, MATLAB catches the error when the handle is evaluated by feval. MATLAB allows you to assign an invalid handle and use it in such operations as func2str, but will catch and report an error when you attempt to use it in a runtime operation. For example,
fhandle = @no_such_function; func2str(fhandle) ans = no_such_function feval(fhandle) ??? Error using ==> feval Undefined function 'no_such_function'.
Including Path In the Function Handle Constructor
You construct a function handle using the at sign, @, or the str2func function. In either case, you specify the function using only the simple function name. The function name cannot include path information. Either of the following successfully creates a handle to the deblank function.
The next example includes the path to deblank.m, and thus returns an error.
fhandle = str2func(which('deblank')) ??? Error using ==> str2func Invalid function name 'matlabroot\toolbox\matlab\strfun\deblank.m'.
Evaluating a Nonscalar Function Handle
The feval function evaluates function handles only if they are scalar. Calling feval with a nonscalar function handle results in an error.
| Saving and Loading Function Handles | Historical Note - Evaluating Function Names | ![]() |