How to communicate two matlab gui?

6 次查看(过去 30 天)
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 个评论
Adam
Adam 2017-1-31
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 2017-2-1
Thanks....!!! I am using setappdata and getappdata command to solve it

请先登录,再进行评论。

采纳的回答

Jan
Jan 2017-1-31
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 个评论
Adam
Adam 2017-1-31
编辑:Adam 2017-1-31
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.

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by