Introduction

Matlab Logo In this tutorial, you will learn how to create a reset button for a GUI. A reset button can be very useful as it quickly returns everything on the GUI to a default value. There are two main ways that I can think of to implement a reset button, and in this tutorial I will introduce one of the methods. I will cover the other method in a different 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. Next, we must allow the GUI to run multiple instances. Go to the Tools tab, and then to GUI Options. Disable the following option as shown:

    GUI Skeleton

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

  7. Find the reset_Callback and add the following code

    closeGUI = handles.figure1; %handles.figure1 is the GUI figure
     
    guiPosition = get(handles.figure1,'Position'); %get the position of the GUI
    guiName = get(handles.figure1,'Name'); %get the name of the GUI
    eval(guiName) %call the GUI again
     
    close(closeGUI); %close the old GUI
    set(gcf,'Position',guiPosition); %set the position for the new GUI
  8. Now run the GUI and test it out! You should see a brief flicker when you reset the GUI, as a new GUI is being opened while the old one is closed.

Conclusion

In this tutorial, you learned how to implement a reset button. Although the flickering can be a tad annoying when the GUI is reset, it gets the job done. It should be noted that multiple copies of the same GUI is now possible since we changed the option to allow multiple copies of the GUI to run. Thus, if you call the GUI twice, you will get two copies of the GUI to run simultaneously!

The next GUI tutorial will show you how to implement a reset button by creating an m-file to initialize all the values within the GUI.

There are probably other ways that a reset button can be implemented. Please share your ideas!

Download the Source Files

Click here to download the source files.

This is the end of the tutorial.