C = {@sin, @cos, @tan};
C{2}(pi)
ans =
-1
Or use a structure array:
S.a = @sin; S.b = @cos; S.c = @tan;
S.a(pi/2)
ans =
1
Saving and Loading Function Handles
You can save and load function handles in MATLAB, as you would any other variable. In
other words, use the save and load functions. If you save a function handle, MATLAB
does not save the path information. If you load a function handle, and the function file no
longer exists on the path, the handle is invalid. An invalid handle occurs if the file location
or file name has changed since you created the handle. If a handle is invalid, MATLAB
might display a warning when you load the file. When you invoke an invalid handle,
MATLAB issues an error.
See Also
func2str | functions | isa | str2func
Related Examples
- “Pass Function to Another Function” on page 13-6
More About
- “Anonymous Functions” on page 20-24
See Also