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

10 views (last 30 days)
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.

Answers (5)

Matt Fig
Matt Fig on 9 Jun 2011
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 on 29 May 2013
This video gets me understand and solve this problem. Hope it helps you to. download submission. to view.

Sean de Wolski
Sean de Wolski on 9 Jun 2011
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 Comments
Sean de Wolski
Sean de Wolski on 9 Jun 2011
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 on 9 Jun 2011
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

Sign in to comment.


Brittany
Brittany on 10 Jun 2011
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 on 17 Aug 2011
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?

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!