Introduction

Matlab Logo 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!

Main GUISubGUI

The Example

  1. First, download the GUIs here. Unzip the files and place them wherever you please.

  2. Now, type guide at the command prompt.

    Command prompt

  3. Choose to open the sample GUIs by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

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

    main gui guidesub  GUI guide

  5. Click on the mfile icon icon on the GUI figure to bring up the accompanying .m file.

  6. 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);
  7. 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();
  8. 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);
  9. 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);
  10. 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.