Multiple Callback Functions

22 vues (au cours des 30 derniers jours)
Jonas Reber
Jonas Reber le 1 Juil 2011
Hello MatLabers
I'm trying to have multiple callback functions executed on a uicontrol callback function.
Usually I go something like this:
obj = uicontrol(...,'style','popupmenu',...
'Callback',@(h,e)this.myfunc(h));
now I would like to not only have myfunc to be executed, but also myfunc2 and myfunc3.
I've tried the following (cell array of function handles):
obj = uicontrol(...,'style','popupmenu',...
'Callback',{@(h,e)this.myfunc(h), ...
@(h,e)this.myfunc2(h), ...
@(h,e)this.myfunc2(h)});
which does not work.
Any idea on how to achieve this? I explicitly do not want to have myotherfunc (which calls myfunc, myfunc2, myfunc3) to be the callback but rather be able to directly assign them to the uicontol (or whatever other callback function)
Thank you
  1 commentaire
Jonas Reber
Jonas Reber le 1 Juil 2011
note:
I'm doing OOP with handle classes and "this" refers to the object itself.
e.g.
classdef MyClass < handle
...
methods
...
function this = myfunc(this, hObject)
this.whatever = get(hObject,'property');
end
...

Connectez-vous pour commenter.

Réponse acceptée

Daniel Shub
Daniel Shub le 1 Juil 2011
It is ugly ...
obj = uicontrol(...,'style','popupmenu',...
'Callback', @(h,e)(cellfun(@(x)feval(x,h,e), ...
{@(h,e)this.myfunc(h), ...
@(h,e)this.myfunc2(h), ...
@(h,e)this.myfunc2(h)}))
  2 commentaires
Jonas Reber
Jonas Reber le 5 Sep 2011
ugly but does exactly what I was looking for. Thanks
Muhammad Tauqeer
Muhammad Tauqeer le 30 Oct 2016
exactly looking for this beauty thank you!!

Connectez-vous pour commenter.

Plus de réponses (4)

Friedrich
Friedrich le 1 Juil 2011
I think this is not possible without using some eval and feval command. I would suggest using only one callback function which calls the other functions:
obj = uicontrol(...,'style','popupmenu',...
'Callback',{@(h,e)this.dummyfunc(h)});
function dummyfunc(h)
this.myfunc(h);
this.myfunc2(h);
this.myfunc3(h);
end
  1 commentaire
Raymond
Raymond le 13 Juil 2012
hi..sorry but what is this @(h,e) means and stands for???

Connectez-vous pour commenter.


Titus Edelhofer
Titus Edelhofer le 1 Juil 2011
Hi,
perhaps you could explain why "myotherfunc" would be bad. Perhaps events could help you out (the callback emits an event and you have three listeners who react to this event...?)
Titus

Jan
Jan le 1 Juil 2011
I'd create a single callback, which calls the different functions. And if the list of callbacks must be created dynamically, a cell of function handles can be used as input for this wrapper:
fcnList = {this.myfunc, this.myfunc1, this.myfunc2};
obj = uicontrol(...,'style','popupmenu',...
'Callback', {@wrapper, fcnList});
function wrapper(ObjH, EventData, fcnList)
for iFcn = 1:length(fcnList)
feval(fcnList{iFcn}, ObjH, EventData);
end

Thomas Merlo
Thomas Merlo le 16 Fév 2021
try putting all the function names in a single string:
hMenu = gcf;
set(hMenu,'MenuBar','none');
h = uimenu(hMenu,'Label','Multiple Callbacks', 'Callback', 'FuncA; FuncB; FuncC;');
----------
Obviously, you'll want to define these example furnctions first; for A, B, and C, something like:
function FuncA
fprintf('FuncA\n');
return
~ "works on my machine".

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by