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

2 次查看(过去 30 天)
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);
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

更多回答(0 个)

类别

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