Matlab - Check if function handle is a particular function or function type
Asked Answered
M

1

8

Question: In Matlab, how can I check if a function handle is a particular function or function type?

Example: Let f1 be a function handle. How do I check if f1 is the in-built Matlab function mean? How do I check if f1 is an anonymous function?

My Current Solution: My current solution to this problem involves a call to the functions function. functions accepts a function handle as input and returns a structure containing information about the input function handle, eg function type, path, function name etc. It works, but it is not an ideal solution because, to quote the official documentation:

"Caution MATLAB® provides the functions function for querying and debugging purposes only. Because its behavior may change in subsequent releases, you should not rely upon it for programming purposes."

Morville answered 12/8, 2013 at 9:30 Comment(4)
This is a valid question in the abstract, but I wonder why you need to do this? What's the bigger thing behind the question that you're trying to do?Misti
@SamRoberts Within a function that accepts f1 as one of its inputs, I am able to take certain short-cuts if f1 is one of a number of recognized functions, such as a sample mean etc. However, I also want the function to work for other types of functions f1 where such short-cuts are impossible. Does this make sense?Morville
Colin, you might like to read through this article, particularly the two comments (one quoted, one in the actual comments) from MathWorks' Loren Shure: undocumentedmatlab.com/blog/… It would indicate that some builtin functions such as cellfun face the same issue as you, and solve the issue by allowing both function handles and a limited set of string arguments in the same place, providing an alternative and faster code path for the preset strings. Just a thought.Misti
@SamRoberts That's a very interesting read, thank you - it really reinforces the point you made earlier about duplicate function names. Good to see that I'm not the only one trying to deal with this problem :-)Morville
P
11

How about using func2str?

If this is an inbuilt function, it should just return a string containing the function name; if it is an anonymous function it should return the anonymous function (including @).

h1 = @(x) x.^2;
h2 = @mean;
str1 = func2str(h1);  %str1 = "@(x) x.^2"
str2 = func2str(h2);  %str2 = "mean"

You can also use isequal to compare two function handles (ETA: this will not work to compare two anonymous functions unless one was created as a copy of the other):

isequal(h1,@mean);  % returns 0
isequal(h2,@mean);  % returns 1
Plankton answered 12/8, 2013 at 9:51 Comment(2)
You need to be careful with this - it will not work if the path is changing. For example: create a user-defined function mean on the path. Make a handle to it with f=@mean. Delete the user-defined function. Make a handle to the built-in mean with g=@mean. Now func2str(f) and func2str(g) both return mean. Also (this seems like a bug to me) isequal(f,g) returns true. But they are different things, and f will now error as the file is not accessible. If you need to worry about path changes or function shadowing like this, I don't think what you're asking for is fully possible.Misti
@SamRoberts Interesting point. It isn't a problem for me personally - I studiously avoid both run-time path changes and duplicate function names - but your comment is definitely a valuable resource for others interested in this topic. Many thanks.Morville

© 2022 - 2024 — McMap. All rights reserved.