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."

horchler
  • 18,384
  • 4
  • 37
  • 73
Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
  • 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? – Sam Roberts Aug 12 '13 at 10:53
  • @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? – Colin T Bowers Aug 12 '13 at 14:25
  • 2
    Colin, you might like to read through this article, particularly the two comments (one quoted, one in the actual comments) from MathWorks' Loren Shure: http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/ 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. – Sam Roberts Aug 14 '13 at 08:16
  • @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 :-) – Colin T Bowers Aug 15 '13 at 01:44

1 Answers1

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
nkjt
  • 7,825
  • 9
  • 22
  • 28
  • 5
    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. – Sam Roberts Aug 12 '13 at 14:41
  • 1
    @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. – Colin T Bowers Aug 14 '13 at 01:27