Multiple Callback Functions

53 visualizaciones (últimos 30 días)
Jonas Reber
Jonas Reber el 1 de Jul. de 2011
Respondida: Thomas Merlo el 16 de Feb. de 2021
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 comentario
Jonas Reber
Jonas Reber el 1 de Jul. de 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
...

Iniciar sesión para comentar.

Respuesta aceptada

Daniel Shub
Daniel Shub el 1 de Jul. de 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 comentarios
Jonas Reber
Jonas Reber el 5 de Sept. de 2011
ugly but does exactly what I was looking for. Thanks
Muhammad Tauqeer
Muhammad Tauqeer el 30 de Oct. de 2016
exactly looking for this beauty thank you!!

Iniciar sesión para comentar.

Más respuestas (4)

Friedrich
Friedrich el 1 de Jul. de 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 comentario
Raymond
Raymond el 13 de Jul. de 2012
hi..sorry but what is this @(h,e) means and stands for???

Iniciar sesión para comentar.


Titus Edelhofer
Titus Edelhofer el 1 de Jul. de 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 el 1 de Jul. de 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 el 16 de Feb. de 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".

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by