Correct way to test if a value is a function?

I'm using the following test to check if a value is a function (standard or anonymous):
if isa(value, 'function_handle')
disp('Yeah bro');
else
disp('Nah bro');
end
This satisfies me for everything I've thrown at it so far, and it's pleasing to have code that speaks my regional dialect... I just wanted to check that this is the accepted way to do it in MatLab.
I didn't see an equivalent function (as there is for ischar, iscell and the like) but that could be because it's not a very common thing to do.
Out of indulgent curiosity, is there a test to see if a function is anonymous or not? Forget about whether there's a real-world use case for it. I decided that this works:
if isa(value, 'function_handle')
if strncmp(char(value), '@', 1)
disp('Anonymous as, bro');
else
disp('Average as, bro');
end
end
Obviously all this could be wrapped up into helpful little bundles devoid of Kiwi dialect...
isfun = @(f) isa(f, 'function_handle');
isstdfun = @(f) isfun(f) && ~strncmp(char(f), '@', 1);
isanonfun = @(f) isfun(f) && strncmp(char(f), '@', 1);
Anyone able to make this more concise? And is there an official word to describe functions declared with the function keyword? I kinda thought 'explicit' would suit.
Cheers =)

6 Comments

You've been internalising a really complicated situation in your head, bro
You know I can't grab your ghost chips!!
For the benefit of anyone who is going 'huh?': http://www.youtube.com/watch?v=d8gSL-LJ0iw
How about "functions that you write and execute as a file"? (From http://www.mathworks.com/help/techdoc/matlab_prog/f4-73019.html)
Hahaha, very terse. Anyway, you win! Following the 'function_handle' link from that page, I found the comment:
Use isa(h, 'function_handle') to see if variable h is a function handle.
I guess that makes it official.
I would replace char(f) by func2str(f), because it communicates the intent better. And the test strncmp(char(f), '@', 1) by strncmp( func2str(f), '@(', 2), because every(?) "string of a function_handle" starts with a "@".
I think the char() conversion does exactly that: sees I have passed a function handle and calls func2str(). Calling on one of my own file-defined functions, it does not prefix with @ (nor does it prefix built-in functions). But thank you for pointing out the correct function to use =)

Sign in to comment.

Answers (2)

To test if a function is defined in a file with the keyword function, test the output of exist().
v = exist('some_script') %v = 1;
v = exist('some_function') %v = 2

3 Comments

Thanks Sean. I want to test the contents of a variable. This code only works for functions defined in files, and seems less specific than my example. To use 'exist' I have to convert the value to a string (so if my value is a struct, I'll get an error), and I have to handle an extra value to catch built-in MatLab functions.
Hi Geoff, This was just to answer the last question in your question suite:
"And is there an official word to describe functions declared with the function keyword? I kinda thought 'explicit' would suit"
Oh right... I was actually talking about a word, and not code. Being a programmer, I find it frustrating to have hazy or verbose descriptions of an established programming construct such as "function that is defined in a file as opposed to being in-built or anonymous". So I meant that if I was going to write code to separate these out: "is_anonymous_function", "is_matlab_function", "is_????_function", then what word would best fit the ????. Are you saying it should be 'script'?

Sign in to comment.

If you are debugging (note: don't use this in "production" code, as called out in the Note on its documentation page) you could use the functions function to determine if a variable is a function handle or not.
f = @sin; % "named" or "simple" function handle
check1 = isa(f, 'function_handle')
check1 = logical
1
info1 = functions(f)
info1 = struct with fields:
function: 'sin' type: 'simple' file: ''
g = @(x) sin(x); % anonymous function
check2 = isa(g, 'function_handle')
check2 = logical
1
info2 = functions(g)
info2 = struct with fields:
function: '@(x)sin(x)' type: 'anonymous' file: '/tmp/Editor_iexhj/LiveEditorEvaluationHelperEeditorId.m' within_file_path: '' workspace: {[1x1 struct]}
If the variable you pass into functions isn't a function handle, it will throw an error.
notFunctionHandle = 1:10;
functions(notFunctionHandle)
Incorrect number or types of inputs or outputs for function functions.

Categories

Asked:

on 3 May 2012

Answered:

on 20 Dec 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!