Matlab - check if a function descriptor is a specific function or function type

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

Example: Let f1 be a function descriptor. How to check if there is f1 built-in Matlab mean function? How to check if f1 an anonymous function?

My current solution: My current solution to this problem is to call the functions functions . functions accepts the function descriptor as input and returns a structure containing information about the descriptor of the input function, for example, function type, path, function name, etc. It works, but this is not an ideal solution, because, quoting :

"Caution MATLAB® only provides functions for queries and debugging. Since its behavior may change in future releases, you should not rely on it for programming purposes."

+8
matlab function-handle
source share
1 answer

How about using func2str?

If it is a built-in function, it should simply return a string containing the name of the function; if it is an anonymous function, it should return an 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 descriptors (ETA: this will not work to compare two anonymous functions, unless they were created as a copy of the other):

 isequal(h1,@mean); % returns 0 isequal(h2,@mean); % returns 1 
+11
source share

All Articles