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')
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
fake_event = struct('Character', 'A', 'Modifier', 'shift', 'Key', 'a', 'Source', item_handle);
... 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:
function FrodosLunchGuide_edit3_Callback(hObject, event, handles)
select_fifth_cheese(handles);
end
function FrodosBreakfastGuide_push5_Callback(hObject, event, handles)
other_handles = guidata( findobj('Tag', 'FrodosLunchGuide_edit3') );
select_fifth_cheese(other_handles);
end
Best Answer