Introduction

Matlab Logo 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.

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. Find the reset_Callback and add the following code

    reset_gui; %calls the reset_gui m file
  7. 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
  8. 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.

  9. 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.