Multiple Callback Functions

22 views (last 30 days)
Jonas Reber
Jonas Reber on 1 Jul 2011
Answered: Thomas Merlo on 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 Comment
Jonas Reber
Jonas Reber on 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
...

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (4)

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

Sign in to comment.


Titus Edelhofer
Titus Edelhofer on 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 on 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 on 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".

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!