How to compare function handles?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm getting callbacks from a MEX file. On the matlab side I'd like to manage a list of matlab function callbacks, or function handles, to be called when an event is triggered from my MEX file.
I am already able to get the callback from my MEX file and call all the matlab callbacks I store in a list. I'm just not able to remove them from my list.
Consider the following pseudo code.
classdef MyDevice
methods
function self = MyDevice()
% Takes care of initialization and set privateCallback as
% unique callback for all the event name in the MEX file
end
function addCallback(self, event, callback)
% Add to callback map container
end
function removeCallback(self, event, callback)
% Remove callback from map container
end
function testEvent(self, eventname)
% trigger event in the MEX file
% mex file calls privateCallback
end
function privateCallback(self)
% iterate through the callback list
end
end
end
Test callbacks
function myCallback1(eventname)
% ...
fprintf('myCallback1\n');
end
function myCallback2(eventname)
% ...
fprintf('myCallback2\n');
end
Test code and expected prints:
d = MyDevice();
d.addCallback('channel1', @myCallback1);
d.addCallback('channel1', @myCallback2);
d.test('channel1');
>>myCallback1
>>myCallback2
d.removeCallback('channel1', @myCallback1);
d.test('channel1');
>>myCallback2
Everything works until >>myCallback2.
At d.removeCallback('channel1', @myCallback1), how can I check if the callback in removeCallback callback argument is the same than the one previously set in the first addCallback?
isequal works only with named functions and it appears that each function handles are unique.
Thanks!
0 Kommentare
Antworten (1)
Adam
am 20 Jan. 2017
Bearbeitet: Adam
am 20 Jan. 2017
doc func2str
will give you a string representing your function that you can compare, but it depends how complex your functions are and what you consider to be 'equal'.
e.g.
>> f = @(x) x.^2;
>> g = @(x) x.^2;
>> isequal( f, g )
ans =
logical
0
>> strcmp( func2str( f ), func2str( g ) )
ans =
logical
1
If your function handle is to some long function though I'm not sure what this will look like and whether it works. Theoretically it would as it will test that the code being executed by the two function handles is the same, irrespective of the name of the function or its variable. Actually if the function handle is to another named function then func2str will just give the name of that function anyway, not convert its whole function body to a string.
4 Kommentare
James Tursa
am 20 Jan. 2017
Function handles take shapshots (i.e., shared data copies) of the workspace variables they use at the time of the function handle creation. So there is really no way to reliably connect them at a later time to any currently existing workspace variables for some type of comparison purpose. I wouldn't even know how to hack into them at the mex level for something like this. I think the only reasonable thing to do is what Adam suggests ... have simple function handles with only a function name that can be converted to a string for comparison purposes ... not anonymous function handles with embedded code & variables.
Walter Roberson
am 20 Jan. 2017
Bearbeitet: Walter Roberson
am 20 Jan. 2017
At the MATLAB level, if you have two anonymous functions with identical strings, but there are bound variables, then functions() 'workspace' field will isequal() for the two if the values (content) of the bound variables is the same, even if the variables were reassigned and assigned back in-between.
That is, isequal() for this purpose will not detect that the storage locations are the same, just that the values are the same. It does not even detect that the bound variables are in the same context, since isequal() of struct is mostly defined to compare by value not by location.
I have to ponder, though, that two callbacks with different strings are not necessarily going to execute to different results, due to aliasing, so it is not clear to me that your API is well defined.
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!