How can I call a callback within a callback from a different GUI
2 views (last 30 days)
Show older comments
I have two GUIs, Agui & Bgui. How can I execute a callback for Agui from a callback in Bgui? Thanks
0 Comments
Accepted Answer
Walter Roberson
on 15 Apr 2016
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);
else
error('callback was wrong class, was %s', class(cb));
end
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 http://www.mathworks.com/help/matlab/ref/figure-properties.html#property_windowkeypressfcn
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)
select_fifth_cheese(handles);
end
%the other GUI
function FrodosBreakfastGuide_push5_Callback(hObject, event, handles)
other_handles = guidata( findobj('Tag', 'FrodosLunchGuide_edit3') );
select_fifth_cheese(other_handles);
end
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!