Introduction

Matlab Logo In this tutorial, you will learn how to save data from your GUI, and how to load it back in. This is a useful feature when you want your GUI to “remember” a group of settings. In addition, this gives you the chance to go back to previous states. The example we will use in this tutorial involves the slider GUI that was created in the Slider Tutorial. Here is a quick look at the finished GUI.

Finished GUI

The Example Files and Code

  1. First, download the GUI skeleton 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 GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  4. Here is what the GUI should look like when you open it:

    GUI Skeleton

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

  6. Let’s add some code to each of the pushbuttons. First, lets add the code for the save button.

    Find the save_pushbutton_Callback and add the following code

    %allow the user to specify where to save the settings file
    [filename,pathname] = uiputfile('default','Save your GUI settings');
     
    if pathname == 0 %if the user pressed cancelled, then we exit this callback
        return
    end
    %construct the path name of the save location
    saveDataName = fullfile(pathname,filename); 
     
    %saves the gui data
    hgsave(saveDataName);
  7. Next, we want to add the following code to the load_pushbutton_Callback:

    %allow the user to choose which settings to load
    [filename, pathname] = uigetfile('*.fig', 'Choose the GUI settings file to load');
     
    %construct the path name of the file to be loaded
    loadDataName = fullfile(pathname,filename);
     
    %this is the gui that will be closed once we load the new settings
    theCurrentGUI = gcf;  
     
    %load the settings, which creates a new gui
    hgload(loadDataName); 
     
    %closes the old gui
    close(theCurrentGUI);
  8. You might have noticed there were more callbacks, but you can ignore those for now. Let’s run the GUI! You should see the following GUI appear when you run it:

    GUI Figure

  9. Now, lets move the slider all the way to the right, like this:

    Move Slider

  10. Now, let’s save the GUI’s current state. Press the “Save GUI Settings” button. The following window should appear, asking where to save the GUI data.

    Save GUI data

  11. Next, move the slider to any arbitrary location. Now, click on the “Load GUI Settings”. Choose the same file that you just saved.

    Load GUI data

  12. You should see a brief flicker, and the GUI should be restored to exactly how you just saved it seconds ago. If you moved the GUI to another location on the screen after the save, it will be sent back to the position it was at when you saved it!

  13. You probably noticed the menu on the upper left of the GUI. If you don’t know how to create a custom menu, I suggest you visit this tutorial first. Anyhow, we can go back to the m-file and add in some code for the menu item callbacks. I prefer to use menu items sometimes because they help keep the GUI looking clean and uncluttered.

    Menu

    Copy the same code from the save_pushbutton_Callback to the saveSettings_Callback.

    Similarly, do the same for load_pushbutton_Callback and loadSettings_Callback.

  14. Now run the GUI again, and try using the menu instead. It should work exactly the same! In addition, you can even try the hotkeys, (Control + S) to save, and (Control + L) to load.

Download the Source Files

Click here to download the source files.

This is the end of the tutorial.