MATLAB GUI - Using a Sub GUI to Manage Setting Parameters
16 Sep 2008 Quan Quach 52 comments 6,911 views
Introduction
This tutorial focuses on using a sub GUI to change setting parameters on your main GUI. The sub GUI is called from a main GUI and allows the user to change particular settings. This can be extremely useful when you don’t want to display all your settings on the main GUI, saving precious GUI space. There has been a lot of confusion in the tutorial i presented here about sharing data between two GUIs. Hopefully this will clear things up! Special thanks goes to Denis for pointing out a more elegant way of getting done!


The Example
-
First, download the GUIs here. Unzip the files and place them wherever you please.
-
Now, type
guideat the command prompt.
-
Choose to open the sample GUIs by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
The two GUIs should look like this when you open them:


-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
First, lets go over the following snippets of code in the main GUI. This first part is the code is for the add_pushbutton within the main gui.
function add_pushbutton_Callback(hObject, eventdata, handles) %get the values from each of the inputs handles.first = str2num(get(handles.input1, 'String')); handles.second = str2num(get(handles.input2, 'String')); handles.third = str2num(get(handles.input3, 'String')); %add up the three numbers total = handles.first + handles.second + handles.third; %display the total value set(handles.answer_text,'String',num2str(total)); guidata(hObject, handles);
- The next snippet of code calls the sub gui from the main gui. As you can see, the code is pretty simple.
function change_settings_pushbutton_Callback(hObject, eventdata, handles) %call the sub gui sub_gui();
-
Now, lets look at the code that goes into the sub GUI. This is where it gets interesting! The edit callbacks are pretty self explanatory. This is done for all three of the edit text fields.
function edit1_Callback(hObject, eventdata, handles) %gets the contents of the edit text field %and then converts it into a numeric value %if not a number then input will be empty input = str2num(get(hObject,'String')); %checks to see if input is empty. if so, default input1_editText to zero if (isempty(input)) set(hObject,'String','0') end guidata(hObject, handles);
-
The save button callback is the most involved so pay special attention! Notice how we save the data back into the main gui!
% get the main_gui handle (access to the gui) mainGUIhandle = main_gui; % get the data from the gui (all handles inside gui_main) mainGUIdata = guidata(mainGUIhandle); % change main gui strings set(mainGUIdata.input1, 'String', get(handles.edit1, 'String')); set(mainGUIdata.input2, 'String', get(handles.edit2, 'String')); set(mainGUIdata.input3, 'String', get(handles.edit3, 'String')); % save changed data back into main_gui %this line updates the data of the Main Gui guidata(main_gui, mainGUIdata); % close this gui close(sub_gui);
-
And there you have it! Download the source files and give it a try! Now you should be able to create a sub GUI that can change the settings on your main GUI!!
Bonus
When you call the sub GUI, it can be annoying if the sub GUI appears right on top of the main GUI. To prevent this, you can designate the position of the sub GUI when it appears. In the opening function of the sub GUI, you can do the following:
% --- Executes just before sub_gui is made visible. function sub_gui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to sub_gui (see VARARGIN) % Choose default command line output for sub_gui handles.output = hObject; mainFigureHandle = main_gui; %stores the figure handle of Quan's GUI here mainGUIdata = guidata(mainFigureHandle); mainGUIposition = get(mainGUIdata.figure1,'Position'); %get position of the main gui subGUIposition = get(handles.figure1,'Position'); %get position of sub gui subGUIposition = [mainGUIposition(1)+mainGUIposition(3),subGUIposition(2),subGUIposition(3),subGUIposition(4)]; %set the new position of the sub gui set(handles.figure1,'Position',subGUIposition); % Update handles structure guidata(hObject, handles);
Download the Source Files
You can download the source files here.
52 Responses to “MATLAB GUI - Using a Sub GUI to Manage Setting Parameters”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


[...] Note: If you want to use a sub GUI to change settings parameters on a main GUI, check out this tutorial instead! [...]
downloading this file only downloads the sharing data between two gui’s file. Am I doing anything wrong?
I believe those are the right files. Run the main gui first and press the “change settings” button to bring up the sub gui.
I’m also having a problem downloading the files. It looks like its downloading the files used in sharing between two GUI’s help where you shared the input between daniel and Quan’s GUIs
Sorry guys, I guess I didn’t update the link!! My apologies. Link is now working properly
Hi,
I was using your code but i’m experiencing a small difficulty. After I activate the sub gui and click “save settings,” the main gui regenerates itself. By the time i am done saving each parameter on the sub gui, i have many instances of the main gui open. i have looked over the code but i am not sure what the problem might be.
Hi Joseph,
Not sure what your problem is since it works perfectly fine on my desktop. Are you using the example code provided verbatim? What version of MATLAB?
Quan
Hi,
I have a Main GUI and a Sub GUI of my own.
The Sub GUI loads and image and makes a few measurements.
I need to synchronize it with the Main GUI in such a way that these measurements appear on the Main GUI too, after I finish with the Sub GUI.
How do I go about with this?
The Sub GUI is quite complicated with many buttons of its own. I need to synchronize only the measurements part. Please help me with this.
Thanks!
Hello Pratyusha,
The tutorial explains how to share data between the sub gui and the main gui. I believe you will find your answer if you study the tutorial a little bit more.
Quan
i have some problems in ‘linking’ the interface..i have 3 interface which i created using Gui..and i want them to link together when clicking ‘next’ / ‘back’ button…is it possible?may i know how should i do it?…so that when user input data, we can bring the data to the next interface etc.. please help me…thanks
Hi,
I experience the same problem as I think Joseph asks about. After returning from the subgui to maingui, I’ve momentarily access to the updated variables… but, next time I reach the subgui, I only have access to the original (very first setup) input numbers and not those I changed at previousely access.
I might be wrong, but I didn’t get your code to work (as I want) with the slightly modification that in the subgui, I set the ‘String’ value to whatever value I had stored in ‘UserData’, which should be the previous updated values, the 2nd time I reach the subgui.
Can it have something to do with the handle that is initiating mainFigureHandle = main_gui? That might cause the main_gui OpeningFcn to be run again, and therefore overwriting the updated values. I’ve tried to get around it by using guidata(gco(main_gui)) but it seem to cause the same action happening (if it’s the problem).
Suggestions?
btw.. thanks for a very useful tutorial.
Hi Quan,
Let’s say I have three GUIs named gui1, gui2 and gui3
On gui1, I have a button called NEXT_2 and a button called NEXT_3.
When I press NEXT_2, in order to make gui2 appear, all I have to do is the following: under the callback code of the “next” button, i should simply write.
Is it correct?
Now, I have problem with a process like this:
At first, on gui2, i have a button called SAVE. when i press SAVE, the gui2 should disappears and all collected data in gui2 are saved and displayed in a static text component called display_static_text on gui1.
Secondly, I would like to do the same thing with gui3. And the data displayed in display_static_text on gui1 (mentioned above) should contain both data saved from gui2 and gui3.
I just didnt have the idea how to do this with matlab. Is there any suggestion?
And, thnx for your tutorial. It helps me so much.
Dear Quan and friends,
I am intending to get a String text from ‘main GUI’ and set it on the ’sub GUI’. But I receive an error like :
??? Index exceeds matrix dimensions.
Error in ==> edit_parameters at 52
filename = varargin{1};
Extra info:
I am modifying the code for a simulator of a converter. They are a huge code list, here I just put some line that related to my question.
I am using MATLAB 7.3.0 (R2006b).
Question:
I don’t know why it exceed the matrix dimension and how to change the dimension of the matrix that MATLAB means. Need your assistance here,please!
I look forward to hear from you and others, if you guys have any clue on my problem here.
Thanks & Regards,
Richard N
Hi Richard,
It’s difficult to tell exactly what is going on in your code without seeing all of it. Even then, it would take me a fair amount of time to figure out what is going on. You might want to take a look at this tutorial here:
http://blinkdagger.com/matlab/matlab-gui-how-to-easily-share-data-between-two-separate-guis
Good luck!
Quan
Hi Folks.
I have to say, that your code does -not- work well on my matlab (7.0.1). I did some changes and now I’d like to share my results and optimization of your code. The following lines showing only the changes.
But before this, I’d like to make something clear:
1.
I always use to create variables inside my GUI. As you know, all elements (boxes, edits etc.) are stored inside a handles-structure. So, if these elements are accessable everywere inside this gui, why we shouldn’t use the same handles-structure! And that’s what I do:
e.g. create a variable ‘X = 1337;’ -> ‘handles.X = 1337;’
The simple goal is, to get these handles, modify these and finally save them.
2.
In your program you define a structure in the beginning of your opening function. I think this is one of main problems. Whenever you change via extern gui data from the main gui, matlab will instandly load the openingfunction _again_. So, all your changes are obsolet and gone. The ony reason why your example is working well is because you still access to the sub_gui and there ‘userdata’. There is no changing happening due to the openingfunction behaviour.
Just try it if you put after your changes inside sub_gui an ‘close(sub_gui)’.
However lets see what I have done. Maybe I was wrong
and my code is buggy.
main.gui.m
sub_gui.m
Hope that works. And don’t take it as a bad criticism, your work is good!
Best regards
Denis
Something to add:
If you want to see what is actually happing inside your code:
- set a breakpoint
- run the code
- if the breakpoint is reached, something like ‘K>>’ should be inside your command window
- type ‘handles’
- now it will show you all the handles which are used/stored by the gui.
Bye
Denis
Hi Denis,
Thanks for the code optimization. I would need to go back and look at what I did, but I believe I coded it the way I did for a reason . . . that I can’t seem to remember right now.
After looking at your suggested code change, I found the part where I think it makes things easier:
I’ll test it out when I find a machine with MATLAB on it, but it looks like it does the trick and makes things easier. I will probably modify this tutorial to reflect your suggestions! Thanks, I love learning new things in MATLAB from our readers!
Quan
Hi Quan,
Thanks for your assistance. It works pretty well here. Now, I could continue my work.
Regards,
Richard
Hi Quan,
I am creating a main GUI and it has some sub-GUI. Here, what I’m going to get data from the subGUI, WITHOUT a button. I have seen your sample code, which is access all of the GUIdata. But, when I follow it, it gives me this error:
???Maximum recursion limit of 100 reached.Use set(0,’RecursionLimit’,N) to change
the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
Error in ==> isprop at 22
p =findprop(handle(varargin{1}(i)),varargin{2});
The things is the subGUI indeed has a lot of data. Hence it comes like that.
Need an assistance, please.
Thanks & Regards,
.Richard N.
Halo, this is a nice site, very helpfull for me. But i have a problem when i try to combine this method with your another method to share data among the component callbacks.
In my research, I need to capture a stereo image with 2 until 4 camera, depend on i installed at the first. So, I tried to make a main GUI for select how many camera what i use (with pop menu, selection 1-4 camera). and i made a button for access sub GUI, for make a setting of parameter every camera. This sub GUI function will return a value of video input from every camera (as variable vid1, vid2,…) in matlab workspace (result from sub gui will store at matlab workspace).
The problem is when i press the close button in sub GUI (for activating function store the result to matlab workspace), it always close my main GUI and not close a sub GUI. After that, i can’t use my matlab and matlab must be close by windows task manager. I think is a problem with close handle (i used closereq in my sub GUI). but i am not sure how can fix it.
Can you give me some clue, how can I solve this problem. Thanks for your help.
hi
is it possible for me to display the sub gui inside a designated place in the main gui itself? instead of openning another window for the sub gui?
cause i am writing a program which uses many user inputs so i divided them into several gui’s and a main gui. the main gui contains a list (names of the sub gui’s) and an empty space in the centre for displaying the contents of the sub gui’s when selected from the list.
so i want it such that the sub gui opens in the main gui itself . and when another list item is selected i want the displayed sub gui to close the selected sub gui to appear in its place.
as of now i am only able to program it in such a way that when the list item is selected, the sub gui opens but in another window.
Please help me out
Thanks
Hi everybody!
I have a problem in sharing data between sub guis and main gui.
I have a main Gui with a Menú Editor.
Each menú is aimed to insert input parameters (e.g.: dipole position in x,y,z).
So, when I click on the menú “dipole position”, the main Gui calls a subgui that reads these values and save them.
The problem is that I’m not able to access these values from the main Gui since in the main there are not the objects xd, yd, zd - (I want just to use them in a function for computing the field) - so the commands ” set” and ” get” do not work. I think I would have to use the varargout function to pass the read input parameters as output from the sub gui to the main gui. But I don not know how it works…I hope somebody can help me!!!
I did in this way, more or less:
Subgui:
But it does not recognize the handles of course!
I hope it is enough clear…It’s the first time I’m programming a GUI… :s
Monica,
Please read through this tutorial again. I believe it contains the answer you are looking for.
Quan
Hi Quan!
Thanks for the info.
Actually I read the tutorial again and now I didn’ t get errors in passing data from the SubGuis to the MainGui but it seems it does not read anything (I get all NaN).
Can you suggest me something more, please?
Thanks
Hi Quan!

I solved the problem
Thanks anyway
This web is really well done!
Hallo, i have a question
if i change the code in sub_gui,just like this
i changed the default value from 0 to 1
but when i run the main_gui
it doen’t work
why?
it seems that it didn’t check the input
i hope somebody would help,thanks!!
bob
I mean, i change the default value from 0 to 1 , and by running the GUI just leave one of the inputs blank(without enter any number). but it won’t be changed to 1 automatically.
thanks
bob
@bob
Maybe you meant to do the following:
Hopefully that’ll fix it
thanks for the quick answer,but it still didn’t work.
my question is, how could we set a default value automatically when user doesn’t type any value.
i also tried inserting the code in CreateFcn , still didn’t work, -_-
Bob,
In the opening function is where you want to put code like that.
Each GUI m-file has an opening function that initiailzes the GUI when it boots up.
Inside that function, do the following
cool! it works!! juhu!! thanks thanks thanks!!
Hi..thank you very much this materila is very help to me to create GUI
I had created a simulink model with a subsystem.the subsystem is having an m-file. Now I created GUI with the help of the materials here. Now I want to change one parameter value in the subsystem m file using GUI .Is it possible to change the one parameter value in the m-file from GUI. Could any help me to solve this problem.
this was for amin and sub gui but how can gui be used to display matices of any order and display any changes occuring in them
Hi again,
Sorry but i can’t get past the not having a tag to access. heres what i have
main gui:
now i want the other gui to be able to access the data so it is able to play the audio and plot and edit it. the other gui is editgui and i want to plot the data in an axes called edit1.
I see in the above examplethat there are tags to access and edit but here i don’t have these. I’m confused.
Thanks again
Hello,
I’m trying to modify code to use a dropdown box using a sub GUI (transducer_sub_gui) to change the frequency stored in a main GUI (view1_2). Upon hitting the “choose_frequency” button, i get an error:
??? Error using ==> get
Invalid handle
Error in ==> transducer_sub_gui>choose_frequency_Callback at 130
set(mainGUIdata.transducer, ‘String’, get(handles.transducer,’String’));
My code for the dropdown box transducer_frequency and button choose_frequency are bellow… disp(handles.transducer) works fine, and then it has issues with the next lines. Help?
~Jill
@Jill,
handles.transducer is a string, so I think you should modify the code accordingly:
Hello,
I am new with a designing GUI. Perhaps this is not the right location to put this question but I am facing the same problem constantly when I want to add some modifications to my existing GUI. For instance: I have create a graph and a push button using the guide figure window and subsequently saved my GUI, added a program to plot something in the figure when you push the push button and everything works fine. But then I decide I want to add another buttton to clear my figure and I want to add this button in the guide figure window and save & run the program again. But then my program gets corrupted and I am forced to close Matlab. Is there a way to add components to an existing GUI without destroying the GUI?
Can someone please help me with this (perhaps direct me to an internet page such as this one which explains the above to me?)
I have managed to solve the problem. For those who face the same problem: I opened the existing GUI using guide, adjust the lay-out of the existing GUI, save it and closed this “figure window” (don’t run the GUI directly from the lay-out figure window). Then I open my GUI again in the command window and whoehoe the program works :-)!
There appears to be an error in the code block in step ten.
the line
(sub_gui);
shuold read
(sub_gui);
The uncorrected line calls the OpeningFcn again when executed, causing problems if varargin is used.
Thanks for all the great tutorials!
hi,
nice tutorial.
Is there a way for us to save current string of Edit as default string, moreover, as default string appears next time I run the GUI after closing it?
I am making a GUI that has a parameters screen. The screens are pretty big so I have the parameters screen appear over the main screen. When I have the following line of code in the parameters screen
It ends up putting the main screen in front of the parameters screen (I’m talking about the z order). So I have to click on the toolbar icon for the parameters screen or alt-tab back to the parameters screen several time to update the parameters. It is very annoying.
I am sure that it is this line of code because when I debug step-by-step the main screen comes up when I pass this line.
I have tried making the parameters screen “modal” so that it stays on top but the main screen is persistent and will somehow come in front of it.
Is there some way to make it so that it doesn’t do this? Hopefully somebody has had a similar experience.
Any help is appreciated. THANKS!!!!!
@Brett,
I’ve never had to deal with that before, but a quick search of the help revealed that ‘uicontrol(handle)’ might work if you use a handle name from the GUI you want selected. I haven’t played around with it yet, but just an idea to look into.
GL,
Zane
@Zane
I assume you are talking about the following usage:
%get main gui data mainGUIdata = guidata(MAIN_GUI); uicontrol(PARAMS_GUI);At least, this is what I understood from reading the help (i.e ‘uicontrol(uich)’ gives focus to the uicontrol specified by the handle, uich). In this case it opens up a new instance of of the Parameters screen (which i don’t understand because it is set as “singleton” in the options screen).
Maybe you had something else in mind? It seems like there should be a way to access this stuff without activating the respective figure. Even if I use:
The first line activates the MAIN_GUI. It seems like when it tries to get the handle value it activates the figure….
Thanks for your help though. If you happen to think of anything else I’d be very happy to hear it. This is frustrating.
-Brett
@Zane again
So, I have a hack. I use the following code
that will bring the parameters screen figure to the front. The annoying thing is that the main screen still comes to the front for a second and then matlab switches it back….. it’ll have to work for now.
If anyone has a better fix, i’d like to hear it. Thanks!
I like it Brett. Just because something isn’t graceful doesn’t make it a bad solution.
Quick question if anyone is still reading these comments….
Every time I call
My main gui seems to re-run its opening function.
I am using matlab 7 as my environment. Any idea as to why this is happening?
@Julian
I have been having the same problem. You can see in the several posts before yours. It is really annoying. And I haven’t been able to find the solution yet. My main probelm has to do with the maingui appearing in front of the parameters screen, which is annoying.
Anyway, I found a temporary fix for that. But I would really like to find a way for it not to re-run the opening function EVERY SINGLE TIME.
I have considered something like:
handles.mainFigureHandle = main_gui;in the opening section of the parameters GUI and then just referencing it over and over. I’m not sure if that would solve the problem or not.
I am running 2009a, just so you know.
Hey check out: http://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/
Watching this presentation really helped clear this problem for me.
@ Julian
Great, That video really helped a lot.
His style is quite a bit different from the recommended style on this page. But I think it is better. (no offense blinkdagger) :).
Hi Quan, thanks for your time.
I’ve learned lots of useful tips for gui-ing in this blog. Now, I got one problem.
When clicking on a checkbox to open a sub-gui, the image (jpg) on some axis of the main-gui dissapears. Not the backgroung image, but some other image.
There are no new axes in the sub-gui, I don’t understand it.
¿Any idea?
Thanks.
@Julian, Brett
Hi, I have the same problem, that the main_gui flicks in front of the called gui for just a second before it dissapears - really really annoying.
However, i found the blog here and scrolled down to the commen of Julian; when I click on the link for the video i get to the other homepage, but no video? is there a new link or is it just my web browser (again)?
Thanks for a lot
aspiro
Hi Quan,
Wat if i want to put the data into a giu I have alredy created? and not a sub gui. this is what I want to know. please feel free to drop me a mail thamaranga@hotmail.co.uk
thank you very much
Tam