Invoking parent GUI function from child GUI.

5 views (last 30 days)
Hallo, I have a question regarding passing information and callbacks between parent and child GUIs. By way of a minimal example:
Suppose I have a parent GUI MAIN and a child GUI MOD. I then call the child from its parent. Now MOD will run and do its thing. Eventually it will generate some data, let's say a randomly generated number called Answer. What I want is that as soon as Answer is computed, MOD should be able to pass that value back to MAIN and then call a function in MAIN that reacts in some way to the value of Answer, e.g. squares Answer and displays that on the console.
I must stress that it is important that the parent GUI is called rather than another instance of it. In a more practical example Answer would have to interact with data already in the parent GUI and then update the data in the main GUI for further use.
Is such set-up possible?
Many thanks.
  1 Comment
Alexantrou Serb
Alexantrou Serb on 17 Apr 2015
Edited: Alexantrou Serb on 17 Apr 2015
I found something very related to the answer here:
The procedure revolves around these steps:
1) In the opening function of the main GUI, create a handle to the main GUI for your own convenience:
handles.mainGUIhand = hObject;
2) When calling the child GUI, pass a `dummy' argument and then the handle to the parent GUI. This can be done thus:
h = ChildGUI('DummyArg', handles.mainGUIhand);
3) Now hop over to the child GUI side and grab the input arguments through the opening function. First, look for the dummy argument:
mainGuiInput = find(strcmp(varargin, 'DummyArg'));
4) Then, knowing that the handle to the main GUI follows as the next input argument after `DummyArg', grab a hold of the main GUI's handle:
handles.mainHandCell = varargin{mainGuiInput+1};
Note that varargin is a cell array.
5) Next, grab the handles to the various objects and data within your main GUI:
handles.mainHands = guidata(handles.mainHandCell);
6) Now grab the object whose callback function you want to execute, in this case a push-button called `Example':
push_Example_callback = get(handles.mainHands.push_Example, 'Callback');
7) Finally, call the function to which you just obtained the handle in the previous step. This being a push-button you need to pass it first the handle to the object that is suppose to be calling it (the parent GUI) and then the data of the said object. You can call this function from anywhere you desire (within reason), e.g. in reaction to a push-button or even in the opening function of the child GUI:
push_Example_callback(handles.mainHandCell, handles.mainHands);
A few notes:
a) The various obtained handles in the child GUI were stored in a handles-type variable in my example code so that they can be used by ay function in the child GUI. If you only need to do this from within a certain function within the child GUI, the handles can be stored in non handles-type variables.
b) I suppose that if the function you are after in the main GUI doesn't have a handle that you can grab on to, you can always wrap it inside some form of callback, perhaps to some invisible object? I don't know about that, but would be happy to know if anyone does know.
c) Can this code be made more efficient if we just pass the handle to the bits of data that we need for the program to run successfully rather than passing a handle to the entire workspace of the main GUI in step 5? Will it make much of a difference if we do, or is this handle-passing relatively computationally lightweight?

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 18 Apr 2015
Alexantrou - see http://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s which provides an example of how to pass data between two GUIs. You should be able to adapt the solution found there to meet your needs.

More Answers (1)

Alexantrou Serb
Alexantrou Serb on 20 Apr 2015
Oh, very nice! After a preliminary look it seems like a much more lightweight way to do pass data freely between GUIs.
Thanks.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!