MATLAB GUI - Creating a Reset Button
09 Jul 2008 Quan Quach 5 comments 2908 views
Introduction
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.

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

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

-
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:

-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
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
-
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.
5 Responses to “MATLAB GUI - Creating a Reset Button”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

why am i getting redirected here? i want to view other tutorials!
Quan,
So, what was the other way you were thinking of that resets the GUI? This way seems rather “brute force” to close and open another. My first inclination would be to use *set* commands in the “RESET!” button callback that changed the value properties of the other controls to the defaults.
Rob
Rob,
You are a mind reader. That was exactly what I was going to post tomorrow!
Stay tuned,
Quan
[...] the previous Reset Button tutorial, we discussed how to create a reset button. In this tutorial, we will discuss a different approach [...]
These are fun to make.. And turtie is a master of tutorials!