Programming and Data Types | ![]() ![]() |
Testing for Data Type
The isa
function identifies the data type or class of a MATLAB variable or object. You can see if a variable holds a function handle by using isa
with the function_handle
tag. The following function tests an argument passed in to see if it is a function handle before attempting to evaluate it.
function evaluate_handle(arg1, arg2) if isa(arg1, 'function_handle') feval(arg1, arg2); else disp 'You need to pass a function handle'; end
Testing for Equality
You can use the isequal
function to compare two function handles for equality. For example, you want to execute one particular method you have written for an overloaded function. You create a function handle within a confined scope so that it provides access to that method alone. The function shown below, test_myfun
, receives this function handle in the first argument, arg1
, and evaluates it.
Before evaluating the handle in arg1
, test_myfun
checks it against another function handle that has a broader definition. This other handle, @myfun
, provides access to all methods for the function. If the function handle in arg1
represents more than the one intended method, an error message is displayed and the function is not evaluated.
function test_myfun(arg1, arg2) if isequal(arg1, @myfun) disp 'Function handle holds unexpected context' else feval(arg1, arg2); end
![]() | Converting Function Names to Function Handles | Saving and Loading Function Handles | ![]() |