How can I call a callback within a callback from a different GUI

3 ビュー (過去 30 日間)
John 2016 年 4 月 15 日
コメント済み: John 2016 年 4 月 16 日
I have two GUIs, Agui & Bgui. How can I execute a callback for Agui from a callback in Bgui? Thanks


Walter Roberson
Walter Roberson 2016 年 4 月 15 日
If you create the GUIs using GUIDE, then each will have its own handles structure. handles structures are attached to individual figures. In order for one gui to be able to access the handles of the other gui, it must know or be able to find the handle of at least one object in the other figure.
For example:
myfig = ancestor(hObject, 'figure');
allfig = findobj('type', 'figure');
otherfig = setdiff(allfig, myfig);
otherhandles = guidata(otherfig);
get(otherhandles.edit3, 'String') %or whatever
If you know some unique property of the other figure, you can look for it specifically:
otherfig = findobj('type', 'figure', 'name', 'Frodo''s Guide To Third Breakfasts');
Now that you have the handles of the other GUI, you can fetch the callback properties:
item_handle = otherhandles.edit3;
cb = get(item_handle, 'Callback');
The way to invoke the callback after that depends upon whether it is a string or a file handle or cell array. If it is a handle or cell then it needs to be passed its own handle
fake_event = [];
if ischar(cb)
evalin('base', cb);
elseif iscell(cb)
cb{1}(item_handle, fake_event, cb{2:end});
elseif isa(cb, 'function_handle')
cb(item_handle, fake_event);
error('callback was wrong class, was %s', class(cb));
Notice that above I used fake_event = []. That will work for most callbacks, but some kinds of callbacks such as WindowKeyPressFcn need a structure of information giving details about what is happening. In order to successfully invoke such a callback, you need to create an appropriate struct and pass it in. For example
fake_event = struct('Character', 'A', 'Modifier', 'shift', 'Key', 'a', 'Source', item_handle); %describes a press of a capital A
... Yes, it is this messy! When you execute someone else's callback, you need to create all of the state that would normally be created automatically.
The alternative to this is to use a Java Robot, figure out where the appropriate button or object is on the screen, and have the robot fake the click or keypress on your behalf.
There is a better way, though. If you need the code of a callback to be triggered by a different routine, then put all of the details into a function and have both places call the same function:
%one GUI
function FrodosLunchGuide_edit3_Callback(hObject, event, handles)
%the other GUI
function FrodosBreakfastGuide_push5_Callback(hObject, event, handles)
other_handles = guidata( findobj('Tag', 'FrodosLunchGuide_edit3') );
  1 件のコメント
John 2016 年 4 月 16 日
Thanks. Works perfect. I had to change the GUI options for it to work but all is good now.


その他の回答 (0 件)


Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by