MATLAB GUI - Creating a Reset Button Part II
11 Jul 2008 Quan Quach 19 comments 2,669 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.
19 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!
Thank you in advance ^______________^
Hi, I wanna ask, why my GUI only reset for the axes only? I’ve try several times, but it’s still reset the graph, but not my edit1 and edit2 boxes. Here is my codes:
How can i reset values that are not on the gui itself?
for example
im going to start a program and i want to graph my outputs which will be more than 100. how do i implement the code so that it will take the outputs without having me to put them in in the box value like you have it. Meaning that as soon as i press ’stop’, i want a graph to appear from the data obtained.THen i want to press the ’start’ button to start the program again but it to reset the values and clear the graph at the same time.
Hi everybody!
I have a problem in sharing data between sub guis and main gui.
I have a main Gui with a Menú Editor.
Each menú is aimed to insert input parameters (e.g.: dipole position in x,y,z).
So, when I click on the menú “dipole position”, the main Gui calls a subgui that reads these values and save them.
The problem is that I’m not able to access these values from the main Gui since in the main there are not the objects xd, yd, zd - (I want just to use them in a function for computing the field) - so the commands ” set” and ” get” do not work. I think I would have to use the varargout function to pass the read input parameters as output from the sub gui to the main gui. But I don not know how it works…I hope somebody can help me!!!
I did in this way, more or less:
Subgui:
function subgui_xd_dipole_edit_Callback(hObject, eventdata, handles)
% hObject handle to subgui_xd_dipole_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
user_entry_xd=str2double(get(hObject, ‘String’))*1e-2;
if isnan(user_entry_xd)
errordlg(’You must enter a numeric value’, ‘Bad Input’, ‘ modal’);
end
handles.subgui_xd_dipole_edit=user_entry_xd;
%handles.xd=user_entry_xd;
guidata(hObject,handles);
and son on for y and z.
function save_dipole_pos_Callback(hObject, eventdata, handles)
% hObject handle to save_dipole_pos (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mainGUIhandle = MCF_Simulator;
mainGUIdata = guidata(mainGUIhandle);
(The following commands of course can not work since in the main gui I don’t have the objects xd,yd,zd):
set(num2str(mainGUIdata.xd,’String’), get(num2str (handles.subgui_xd_dipole_edit, ‘String’)));
set(num2str(mainGUIdata.yd,’String’), get(num2str(handles.subgui_yd_dipole_edit, ‘String’)));
set(num2str(mainGUIdata.zd,’String’), get(num2str(handles.subgui_zd_dipole_edit, ‘String’)));
close(MCF_SubGui_dipole_pos);
Then in the MainGui I was trying to do something like:
xd = str2double(get(handles.xd));
yd = str2double(get(handles.yd));
zd = str2double(get(handles.zd));
But it does not recognize the handles of course!
I hope it is enough clear…It’s the first time I’m programming a GUI… :s
is that possible to clear the whole workspace with this reset button?
sometimes, i find out that,it’s hard to clear the variables in workspace within GUI
if our GUI has different callback functions
we send the variables of callback A to workspace
and want to clear the workspace with callback B,with clear all; or clear var;
i tried,but didn’t work, it’s weird
I added a reset pushbuttom to my GUI and I can reset my edit text boxes but I also have some popupmenus that I want to go back to the first case (first option) after the reset pushbutton is selected. What is the code for this?
Thanks for the help. This had been a great website!
Christina, this is all you should need.
set(handles.popupmenu1,’Value’,1);
Zane, Thanks for the reply to both my questions!!!! Both worked perfectly. This is a great website!!!
Christina
hey i want to implement an undo function .which will return to the previous state
i m loading a picture on the axis,then selecting points on it,which will give me the outline of the figure from the point selected,now i want to implement an undo button which will give me only the figure without the outline and i should be able to select the points again
please help
I am creating a GUI, I have 4 ‘EDIT TEXT’ boxes named POS1_1, POS1_2, POS2_1, and POS2_2. Is there a way for me to do this code:
as something like: