Can you program a figure to execute the same callback after each child's callback?

Essentially, I'd like a callback that is for the figure and all of it's children, but is executed last. Of course, you could manually enter this code for every single callback, but perhaps there is a more elegant way?
fh = figure()
button1 = uicontrol('Parent', fh, 'Callback', button1_Callback)
button1 = uicontrol('Parent', fh, 'Callback', button2_Callback)
%...
buttonN = uicontrol('Parent', fh, 'Callback', buttonN_Callback)
function button1_callback(hObject, eventD, fh)
% Do stuff specific to this object.
% Perform this action (***I don't want to type this for every single callback***)
fh_callback(fh, eventD, fh); % Example.
end
function button2_Callback(hObject, eventD, fh)
% Do stuff specific to this object.
% Perform this action (***I don't want to type this for every single callback***)
fh_callback(fh, eventD, fh); % Example.
end
%...
function buttonN_Callback(hObject, eventD, fh)
% Do stuff specific to this object.
% Perform this action (***I don't want to type this for every single callback***)
fh_callback(fh, eventD, fh); % Example.
end

6 件のコメント

At the moment it looks as if you could set all of the callbacks to
@(hObject, event) fh_callback(hObject, event, guidata(hObject))
Instead of using a different one for each?
Walter Roberson
Walter Roberson 2019 年 7 月 11 日
Also I wonder if a uibuttongroup would make sense for you?
For brevity, I made the callbacks all look the same -- the application actually has many different buttons/tools each with their own unique callback functionality.
The task that I'd like them all to have in common, though, is to set the current object of the figure back to the figure itself.
function buttonN_Callback(hObject, eventD, fh)
% Do stuff specific to this object.
% Perform this action (***I don't want to type this for every single callback***)
fh.set('CurrentObject', fh);
end
set(ancestor(hObject, 'figure'), 'currentobject', ancestor(hObject, 'figure'))
But I would just add a function call at the end of each of the callbacks to do this work.
Another approach is
hObject.Enable='disable'
hObject.Enable='on'
That is, disable an object removes its focus.
You could also add a figure window button down callback: that should fire after the individual callback.
Dominik Mattioli
Dominik Mattioli 2019 年 7 月 11 日
Are you sure that a WindowButtonDownFcn callback will fire after each uibutton's callback? My test right now only fires when the mouse clicks on panels, axes.
Walter Roberson
Walter Roberson 2019 年 7 月 11 日
You might need to play with PickableParts

回答 (1 件)

Steven Lord
Steven Lord 2019 年 7 月 11 日

0 投票

Are you setting the figure object's CurrentObject property back to the figure object itself because your callbacks query the CurrentObject to determine what component to query or modify? That could be dangerous -- all it would take would be the user clicking in the wrong place at the wrong time to potentially break your UI by making the CurrentObject not what your callbacks expect it to be.
Instead, I would store the handles of the components in your UI and share the stored handles among the callbacks. That way you don't need to care what the current CurrentObject is, you can refer to either the object whose callback is currently executing (the first input to the callback function) or to a specific object whose handle is in the shared data.

1 件のコメント

Dominik Mattioli
Dominik Mattioli 2019 年 7 月 11 日
編集済み: Dominik Mattioli 2019 年 7 月 11 日
I was asking the question in the abstract because I think it could be helpful to others also, but to be more specific, I am setting the CurrentObject property back to the figure object itself so that clicking on uibuttons (and running their respective callbacks) does not disable quick keys that I've programmed for the UI.
Unless there is a property of uicontrols that prevents the current object from remaining on that button itself (absent a user-intervening mouse click on say, any other non-button object of the UI), this seems like it is necessary.

この質問は閉じられています。

製品

リリース

R2018a

質問済み:

2019 年 7 月 11 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by