How to communicate two matlab gui?

13 views (last 30 days)
VISHWAS CHAVAN
VISHWAS CHAVAN on 31 Jan 2017
Commented: VISHWAS CHAVAN on 1 Feb 2017
Hello, I am creating a maingui and subgui. There is push button in the maingui after clicking it I want to open subgui and then in subgui there is edittext box in which I want to put value after putting the value I want to close that window and want to use that editbox value in the main gui and want to display the calculation under pushbutton of maingui into another textbox. Can anyone suggest the solution using example? Thanks..!!
  5 Comments
VISHWAS CHAVAN
VISHWAS CHAVAN on 1 Feb 2017
Thanks....!!! I am using setappdata and getappdata command to solve it

Sign in to comment.

Accepted Answer

Jan
Jan on 31 Jan 2017
function MainGUI
handles.GuiH = figure('Name', 'Main', 'Tag', 'myMainGUI');
uicontrol('Style', 'PushButton', 'Position', [10, 10, 100, 25], ...
'String', 'Start SubGUI', 'Callback', @StartSubGUI);
handles.TextH = uicontrol('Style', 'Text', 'Position', [10, 50, 100, 25]);
guidata(handles.GuiH, handles); % Store handles struct in figure
end
function StartSubGUI(ButtonH, EventData)
handles = guidata(ButtonH); % Obtain handles struct store in figure
handles2.SubGuiH = figure('Name', 'SubGUI');
handles2.MainGuiH = handles.GuiH;
handles2.MainTextH = handles.TextH;
handles2.TextH = uicontrol('Style', 'Text', 'Position', [10, 50, 100, 25], ...
'Callback', @SubTextCB);
guidata(handles2.SugGuiH, handles2);
end
function SubTextH(TextH, EventData)
handles2 = guidata(TextH);
% Copy the text to the main gui:
set(handels2.MainTextH, 'String', get(TextH, 'String'));
% Store the string in the main gui's application data:
handles = guidata(handles2.MainGuiH);
handles.TextFromSub = get(TextH, 'String');
guidata(handles2.MainGuiH, handles);
end
Now the text typed in the sub GUI is available in the main GUI also, either in the text field or in the handles struct. Choose the solution you prefer.
Alternatively you can use the 'UserData' of the main figure, or the set/getappdata commands. The general idea remains: Provide the handle of the main GUI to the callbacks of the sub GUI.
If you have created the GUIs by GUIDE, forwarding the handles is much harder. Then you can search the handles on the fly:
function SubTextH(TextH, EventData)
MainGuiH = findobj(allchild(groot), 'flat', 'Tag', 'myMainGUI');
if isempty(MainGuiH)
error('Main GUI is not found.');
end
handles = guidata(MainGuiH);
... same as above
  5 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!