MATLAB GUI - Creating a Reset Button Part II
11 Jul 2008 Quan Quach 9 comments 2068 views
Introduction
In the previous Reset Button tutorial, we discussed how to create a reset button. In this tutorial, we will discuss a different approach to creating a reset button. There are pros and cons to both methods, and we’ll go over those in a bit.
In the previous tutorial, we coded a reset button that opened a new instance of the GUI and closed the old instance. In this tutorial, we are going to create a reset button by setting all the pertinent values within the GUI to their default values. In addition, we will clear all axes of their data and labels. For this tutorial, we will be working with the GUI shown below.

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:

-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
Find the reset_Callback and add the following code
reset_gui; %calls the reset_gui m file -
Create an m-file named reset_gui. Make sure it’s located in the same directory as the other two files. This m-file will store all the initialization values for the reset button. When you press the reset button, it will set all the parameters to the values specified in this m-file. Paste the following code into the m-file:
%the following code initializes the relevant parameters to their default values %this is essentially what a reset button does! set(handles.input1_editText,'String','0'); %sets input1 to 0 set(handles.input2_editText,'String','0'); %sets input2 to 0 set(handles.answer_staticText,'String','0'); %sets answer to 0 cla(handles.axes1,'reset'); %clears the axes
-
Note: Instead of creating a separate m-file, we could have just added the previous code directly into the reset_Callback function. Since this GUI is simple, it would have been easier to do it that way. But when you have a larger GUI with many more parameters, it is easier to designate an m-file to initialize your parameters.
-
Now run the GUI and test it out! Input some numbers and add them. Plot the data. Once you are done, try the reset button.
Advantages and Disadvantages
The main advantage of coding the reset button this way is that there is no “flicker” when you activate the reset button. On the flip side, one of the main disadvantages is that you have to hardcode the initialization code for each parameter. If you decide to make modifications to your GUI, you would have to manually change the initialization code to keep up with all the relevant parameters. With the other reset button method, you never have to worry about this.
Conclusion
In this tutorial, you learned a different way to implement a reset button. I prefer using this method to code my reset button because I really don’t like the flickering from the previous method. It may take a little extra diligence, but the end product is superior in my opinion.
Download the Source Files
Click here to download the source files.
This is the end of the tutorial.
9 Responses to “MATLAB GUI - Creating a Reset Button Part II”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

I prefer creating a function such as “GUI_init_state()” that will be executed when the GUI loads and initialize the values of various parameters to wht i want. This is especially useful if u want to have various states of the GUI like: 1) startup, 2) reset, 3) exit, 4) others
Great tutorial, as always!
I’d like to add that another benefit of this method is that it’s easy to add another button to save the current values as a default, then reset to that default. For example:
On initialization, you can make a “default” substructure as part of handles:
…and so forth for all the defaults you need to set
Then your reset_gui function will be:
Then you can add a button to set current values as default, using a callback like:
Plus, if you set up the defaults as a structure, you can use dynamic field names to set/reset the parameters in order to allow easy growth of your GUI:
Hey dp,
Thanks for the added tip. This can save a lot of time when you might need to change your default settings to something else. Great use of structures to make things easier to program.
Quan
Hi Rahul,
Somehow you comment got sent to the spam,
Your method is a good one too! I think that’s how Daniel implements his reset.
Quan
What a nice work this site is.I am an M-beginner.
I have a doubt.
I have a GUIDE application. Now I wanted to uncheck and check the checkboxes by clicking a pushbutton. How can it be done , plz help me.
THANKS IN ADVANCE
Hi guys! I’m new to this site. Chanced upon it while I was looking up online tutorials on how to work the GUIDE on MATLAB and I have to say this site rocks! Great effort!
I have a problem now which I am trying to tackle, it is similar to the reset function and I think the reset function should work in the same way for me. The problem is that I am unable to make dp’s code to work. He says to call a default substructure on initialization but I don’t know where that is.
Also, another issue with his code is that it does not account for cases where the handle.(defnames(vctr)) does not have a ‘String’. But great idea nevertheless. Either that or maybe I’m just not doing it right. More likely the latter.
So what I’ve tried is to place the code
handles.default=handles;at the addinggui_OpeningFcn but it did not enter my edit text box callback named Operator_Callback. This didn’t work as when I used the debug function, the handles structure did not contain default.
Essentially I want to make a code such that if someone enters an incoherent value into a edit text box, the value will reset to the last accetable value entered rather than a default value. Works similarly to how you can use the Esc key in Excel to undo whatever you have entered into a cell as long as you are still in the edit mode.
Also could you explain why the handles.default does not get passed into the function Operator_Callback? I thought the function was meant to pass in the whole structure.
PS. sorry for the messy coding, was just doing wierd things with this code. LOL
Thanks in advance for the help.
Mike
Wow, what a huge comment. Gimme some time to come up with a suitable answer!
Hi Mike,
handles.default=handles;The code above is not going to work but its difficult for me to explain why that is. Let’s just take it at face value.
In regards to DP’s method:
What DP is doing is creating a structure WITHIN the handles structures that stores all the initialization values. For instance, in your code it looks like you have variables a,b, and c and you would like to reset those:
Your initialization structure would be something like:
If you want to default the edit text field to the last known good value, you can do something similar, such as:
Now, in your callback for edit1 you could do:
Good luck with your GUI!
Quan
Woah thanks Quan for the quick reply! It works and I think part of the problem was me not using
Thanks for clarifying my mistake and saving me alot of trouble Quan!
On a side note, I was toying with the idea of creating a GUI with a list box on the left side. If you were to click on any of the objects on the list, it would fill up the empty space on the right side of the GUI with different options depending on the list box object that was selected. Was wondering how to go about doing that. I came up with a few ideas, one would be similar to the reset method number 1 where you just opened a new GUI which had a similar layout.
I suspect that if you want to be able to have that kind of interaction on the GUI itself without using the reset trick, you would have to use ActiveX control. Could you recommend anything to read so I can learn more about it?
P.S. I’ll try to be less liberal with my commenting.
Thanks!