compare anonymous function handles

5 Ansichten (letzte 30 Tage)
Markus Leuthold
Markus Leuthold am 18 Apr. 2016
Kommentiert: Walter Roberson am 22 Apr. 2016
The following comparison of two function handles should return true in my opinion
However, R2016a returns false on this comparison, which is a bug in my opinion. According to the documentation:
Unlike handles to named functions, function handles that represent the same anonymous function are not equal. They are considered unequal because MATLAB cannot guarantee that the frozen values of nonargument variables are the same
In my example, both x and y ARE argument variables. The result is always the same for given arguments. What do you think?

Antworten (2)

Matt J
Matt J am 18 Apr. 2016
Bearbeitet: Matt J am 18 Apr. 2016
The documentation that you quoted is referring to situations like this:
>> a=1; f1=@(x,y)(x*y+a);
>> a=2; f2=@(x,y)(x*y+a);
These display the same,
>> f1,f2
f1 =
f2 =
but give different output for the same input arguments:
>> f1(0,0), f2(0,0)
ans =
ans =
If you consider 2 anonymous functions to be equal if they display the same, you could compare them this way,
>> strcmp(func2str(f1),func2str(f2))
ans =
  1 Kommentar
Markus Leuthold
Markus Leuthold am 22 Apr. 2016
Yes, you're right, the documentation covers the case where you have nonargument values like variable a in your example. Matlab cannot guarantee that a is frozen, as they call it.
However, in my example, Matlab can guarantee the same result for both f1 and f2 with the same input, since I don't have something like variable a from your example. Therefore they should be considererd "equal" in my opinion.
As for func2str(), I cannot use that in my particular use case. I have two huge hierarchical structs to compare. One of the fields contains f1 in the first struct, and f2 in the second struct. I'd like to use
If I'd use instead
where I iterate over all the fields and hierarchy of A and B, then yes, I could use func2str if the datatype of the current field is a function handle and the built-in isequal for all the other datatypes. That's what I am using right now. Unfortunately, this is slower by a factor of 100 compared to the Matlab built-in isequal in my particular use case.

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 18 Apr. 2016
funinfo(1) = functions(f1);
funinfo(2) = functions(f2);
strcmp(funinfo(1).function, funinfo(2).function) && isequal(funinfo(1).workspace{1}, funinfo(2).workspace{1})
  3 Kommentare
Markus Leuthold
Markus Leuthold am 22 Apr. 2016
Interesting solution, I wasn't aware of
However, since I don't have nonargument variables, my local workspace
is always empty. But interesting anyways!
And it still doesn't solve my problem where I have a fcnhandles in deeply nested structs to compare
Walter Roberson
Walter Roberson am 22 Apr. 2016
I doubt Mathworks will change this behaviour any time soon. The alternative would require that Mathworks search all existing function handles, in all scopes, to see if it could find a match.
I suggest you grab a File Exchange Contribution such as and create a routine that takes in strings and return function handles -- we know that you can convert to string since you do not use any variables from your workspace. The routine would keep a copy of each function handle it generates and upon being presented with the same string would return the same function handle, which would then permit isequal() to operate.

Melden Sie sich an, um zu kommentieren.


Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by