Multiple Callback Functions

28 次查看(过去 30 天)
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 个评论
Jonas Reber
Jonas Reber 2011-7-1
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
...

请先登录,再进行评论。

采纳的回答

Daniel Shub
Daniel Shub 2011-7-1
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 个评论
Jonas Reber
Jonas Reber 2011-9-5
ugly but does exactly what I was looking for. Thanks
Muhammad Tauqeer
Muhammad Tauqeer 2016-10-30
exactly looking for this beauty thank you!!

请先登录,再进行评论。

更多回答(4 个)

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

请先登录,再进行评论。


Titus Edelhofer
Titus Edelhofer 2011-7-1
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 2011-7-1
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 2021-2-16
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".

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by