GUI display value from child form to parent form

Hi I have a parent window abc which has a pushbutton1 and a edittext, when I click on this button a new form/window appears with another pushbutton named hi. Now what I want to do it when I press button Hi the message hi should be set to the the edittext in parent window. How can I do it. Please help

6 Comments

You can do this by setting the 'tag' property when you first create the edittext e.g 'tag','parent_edit_tag');
When you want to change it from another process reference the editbox by its tag
set(findobj('tag','parent_edit_tag'),'String','hi')
Is this right "set(findobj('figure1','edit5'),'String','hi');" figure1 is my tagname for gui where I want to display the string in edit text field with tag name edit5
nikhil - if you have created your GUIs with GUIDE, then take a look at https://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s which describes how to pass data between two GUIs.
I want to pass gui data from gui2 to gui1 . Gui2 has a edittext with tagname edit1 and a pushbutton on panel with tag uipanel2. Gui1 has a edittext with tagname edit5. on panel with tag uipanel1 when I enter the text in edit1 text field and press the pushbutton the text entered should be displayed on edit5 text field
Are you using GUIDE? If so, please see the link I provided in my previous comment.
Adam
Adam on 22 Aug 2017
Edited: Adam on 22 Aug 2017
This is the approach I use. It is not trivial, as it uses Object-oriented programming, but it isn't complicated either conceptually.
It is based on the idea that GUIs should never be communicating with each other and taking it upon themselves to give each other instructions.
There should be an underlying program/workflow/set of objects that are communicating and deciding what needs to be done and firing off events/messages. Individual GUIs can then listen for the messages relevant to them and react accordingly. This allows the GUI to be separated from the workflow rather than an integral part of it, so that you could, if wished, just as easily run the workflow without the GUI, just by supplying the paremeters on the command line.
But that is more in the lines of an idealistic approach. If you just want something to work quickly then having GUIs hack into one another and tell each other what to do works...until it doesn't :)

Sign in to comment.

Answers (1)

Jan
Jan on 22 Aug 2017
Edited: Jan on 22 Aug 2017
The general strategy is clear: The 2nd GUI must find the first one, to address the wanted uicontrol. You can either provide the handle of the figure of the 1st GUI during the creation of the 2nd GUI (shown below), or use tags for the recognition.
function CreateGui1
handles.FigH = figure('Tag', 'myGUI1');
handles.edit5 = uicontrol('Style', 'Text', 'String', '');
guidata(handles.FigH, handles);
CreateGui2; % Start the creation from here
end
function CreateGui2
handles.FigH = figure('Tag', 'myGUI2');
handles.button1 = uicontrol('Style', 'PushButton 'String', 'hi', ...
'Callback', @myButtonCallback);
guidata(handles.FigH, handles);
end
function myButtonCallback(ButtonH, EventData)
handles = guidata(ButtonH); % Handles of GUI2!
% Find GUI1:
gui1H = findobj(allchild(groot), 'flat', 'tag', 'myGUI1');
if numel(gui1H) ~= 1 % Either none or multiple
warning('Cannot identify the main GUI uniquely!');
return;
end
% Set the string:
gui1Handles = guidata(gui1H);
set(gui1Handles.edit5, 'String', 'hi');
end
This can be applied to GUIs created by GUIDE also. The dynamic search for the GUI1 fails, if several figures with the same tag are found. Therefore providing the figure handle as input for the creation of the 2nd GUI is smarter:
function CreateGui1
handles.FigH = figure('Tag', 'myGUI1');
handles.edit5 = uicontrol('Style', 'Text', 'String', '');
guidata(handles.FigH, handles);
CreateGui2(handles.FigH); % <== Provide the figure handle
end
function CreateGui2(Gui1H)
handles.Gui1H = Gui1Handle; % <== Store the handle
handles.FigH = figure('Tag', 'myGUI2');
handles.button1 = uicontrol('Style', 'PushButton 'String', 'hi', ...
'Callback', @myButtonCallback);
guidata(handles.FigH, handles);
end
function myButtonCallback(ButtonH, EventData)
handles = guidata(ButtonH); % Handles of GUI2!
handlesGui1 = guidata(handles.Gui1H); % Handles of GUI1!
set(handlesGui1.edit5, 'String', 'hi');
end

Tags

Asked:

on 13 Aug 2017

Edited:

on 22 Aug 2017

Community Treasure Hunt

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

Start Hunting!