How to test/validate a variable is a function handle in matlab ?
it may be something like:
f=@(x)x+1
isFunctionHandle(f)
the is* build-in functions seems not support these kind testing? anyone know? many thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The right way is indeed by means of an
is*function, namelyisa:edit:
For completeness, I’d like to point out that using
class()works for checking if something is a function handle.However, unlike
isa, this doesn’t generalize well to other aspects of MATLAB such as object-oriented programming (OOP) that are having an increasing impact on how MATLAB works (e.g. the plot functionality, the control toolbox, the identification toolbox, … are heavily based on OOP).For people familiar with OOP:
isaalso checks the super types (parent types) of thexobject forsomeClass, whilestrcmp(class(x), 'someClass')obviously only checks for the exact type.For people who don’t know OOP: I recommend to use
isa(x, 'someClass')instead ofstrcmp(class(x), 'someClass')as that is the most convenient (and commonly useful) behavior of the two.