How to communicate two matlab gui?

6 views (last 30 days)
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
Adam
Adam on 31 Jan 2017
That page is full of examples. What are you expecting us to do? Spend our whole morning copying it out from there to here?
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
Adam
Adam on 31 Jan 2017
Edited: Adam on 31 Jan 2017
As for nicer ways to handle things, I use the OOP-based method that I referred to in my comment above (to the original question). I have mentioned it in response to numerous questions, but always saying it was complicated and I didn't have the time to give an example. On the occasion in that thread though I decided to finally put together an example.
I'm sure there are improvements that can be made, but the essence of it is that underlying objects should control the data and calculations and each UI should only be interacting with those, never with each other. It is none of one UI's business what another UI is doing - each reacts to or dictates to the underlying objects.
So if something changes in the 2nd UI, it's callback will change a data property of an underlying object that is also shared with the main UI. The main UI will be listening for changes to this property (or for some event that it triggers) and will react appropriately. Communication in the other direction can be achieved equally.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!