Filter löschen
Filter löschen

Multiple Callback Functions

28 Ansichten (letzte 30 Tage)
Jonas Reber
Jonas Reber am 1 Jul. 2011
Beantwortet: Thomas Merlo am 16 Feb. 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 Kommentar
Jonas Reber
Jonas Reber am 1 Jul. 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
...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

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

Melden Sie sich an, um zu kommentieren.


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

Kategorien

Mehr zu Migrate GUIDE Apps 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