Gui that opens another gui. Need to send variables from one gui to other gui and vice versa

조회 수: 2 (최근 30일)
Right now I have a Main Gui and a Sub Gui. I need two variables from the main gui to copy to my sub gui, and two variables from my sub gui to copy to my main gui. The Main gui has a button called "calculator' that opens my sub gui. The two guis have separate scripts. I have looked at using guidata and sendappdata but I can't seem to find how to tell matlab the variable is in a different script.
I want to send variable a and variable b from my Main GUI to my Sub gui (when I press pushbutton'Calculator') and then variable c and variable d from my Sub GUI to my Main GUI when I press the pushbutton 'OK' in the sub gui. variables a and b are from listboxes in Main gui. variables c and d are from editText boxes in sub gui.
The Main gui stays open when I open the sub gui.
I am not sure what code to include to help with this question, so if you need to see some of my code please ask and tell me what part would be helpful.

답변 (5개)

Matt Fig
Matt Fig 2011년 6월 9일
Here is an example. Adapt it to your needs...
function [] = gui_passdata()
% Pass data back and forth...
S.fh = figure('units','pixels',...
'position',[500 500 200 130],...
'menubar','none',...
'numbertitle','off',...
'name','gui_passdata',...
'resize','off');
S.ed = uicontrol('style','edit',...
'units','pix',...
'position',[10 60 180 60],...
'string','Enter Data, push button.');
S.pb = uicontrol('style','pushbutton',...
'units','pix',...
'position',[10 20 180 30],...
'string','Push to Get Data',...
'callback',{@pb_call,S});
guidata(S.fh,S);
uicontrol(S.ed)
function [] = pb_call(varargin)
% Callback for GUI_24 pushbutton.
S = guidata(gcbf); % Get the structure.
set(0,'userdata',S); % Save it in the root.
f = make_subgui;
% If user closes gui_passdata, close new one as well because it will
% error when it tries to execute the callback otherwise.
set(S.fh,'deletefcn',{@fig_delet,f})
function [] = fig_delet(varargin)
% Executes when user closes gui_passdata.
try
delete(varargin{3})
catch
% Do nothing.
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%SubGUI stuff...
function [f] = make_subgui()
f = figure('units','pixels',...
'menubar','none',...
'position',[750 510 200 100]); % Create a new GUI.
E = uicontrol('style','edit',...
'units','pixels',...
'position',[10 20 180 60],...
'string','Type something, press return.',...
'foregroundcolor','r',...
'callback',{@E_call});
pause(1.25)
S = get(0,'userdata');
str = get(S.ed,'string');
set(E,'string',str,'foregroundcolor','k')
uicontrol(E)
function [] = E_call(varargin)
% Callback for secondary GUI editbox.
S = get(0,'userdata');
set(S.ed,'string',get(gcbo,'string')) % Set gui_passdata editbox string.
close(gcbf) % Close SubGUI.

Firzi Mukhri
Firzi Mukhri 2013년 5월 29일
This video gets me understand and solve this problem. Hope it helps you to. download submission. to view.

Sean de Wolski
Sean de Wolski 2011년 6월 9일
To get you started.
To call a second GUI from the first, just call it's name subgui.m is the name of the subgui, in maingui.m
subgui
  댓글 수: 3
Sean de Wolski
Sean de Wolski 2011년 6월 9일
So you want to set(handles.edit1,'string',num2str(c))
%example edit1 is name of edit box, c is the variable you pulled from appdata.
?
Brittany
Brittany 2011년 6월 9일
I tried that. I cant put it in the sub gui because it doesnt recognize the handle (because the handle is in the main gui). Then I tried putting that set function in the same format you suggested but that doenst work either, Im thinking because I dont have anything telling it to set when I exit the sub gui.
i tried to address that by put this code in the edit box
global c
if isglobal(c) == 1
set(handles.edit1,'String',num2str(c));
end
however this does not work

댓글을 달려면 로그인하십시오.


Brittany
Brittany 2011년 6월 10일
So I actually ended up using 'Buttons'. Its a very useful tool so I thought I'd share. This is good for anyone else trying to have multiple guis share information.
h = get(0,'Children'); %get the children of the display and see which one your other gui is -- my other gui is h(2)
varc = get(handles.SUBGUIeditText,'String');
vard = get(handles.SUBGUIeditText2,'String');
buttons = getappdata(h(2),'Buttons'); %gets all the button functions from main gui and makes them visible to sub gui
set(buttons.mainGUIEdit,'String',(varc));
set(buttons.mainGUIEdit2,'String',(vard));
close SUB GUI;

Nu9
Nu9 2011년 8월 17일
HI, i am doing the same as you but i don´t understand how the second GUI saves the data from the edit text. i need to input 7 variables and then push a buttom to save the values and send it to the main GUI. the result is list off erros :/ i need to declare all the variables?and where a declare them?

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by