Introduction

Matlab Logo This tutorial will show you how to stop a function that is running from within a GUI. Most of the time, when we want to stop a function from running, we simple use the almighty "control + c" combination. But all this really does is kill the entire process thread, which may sometimes be undesirable. This tutorial will show you how to stop a function with the press of a button without killing the GUI. This feature is most helpful when you have long running “for” or “while” loops.

This tutorial is written for those with some experience creating a Matlab GUI. If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab and an understanding on how data is shared among callbacks is highly recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!

The Example Files and Code

  1. First, download the sample GUI 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 Figure

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

  6. Now, add the following code to start_pushbutton_Callback. In order for this interrupt function to work properly, we must use the UserData property. All GUI components, including menus, and the figure have a UserData property. You can assign any valid MATLAB value to the UserData property. Thus, in this case, we are using the UserData property to store the flag variable value.

    %initialize the flag variable
    set(handles.start_pushbutton,'UserData',1); 
     
    %while the flag variable is one, the loop continues
    while (get(handles.start_pushbutton,'UserData') ==1)
        %increments the counter
        temp = str2num(get(handles.counter_text,'String'));
        temp = temp + 1;
        set(handles.counter_text,'String',num2str(temp));
     
        %"flushes the event queue" and updates the figure window
        %since Matlab is a single thread process, this command is requierd
        drawnow
    end
     
    guidata(hObject, handles);
  7. Now, add the following code to stop_button_Callback.

    %toggle the flag variable so that the other process will stop
    set(handles.start_pushbutton,'UserData',0);
    guidata(hObject, handles);
  8. Now, save your .m file and run the GUI. You should see the following GUI appear

    GUI

  9. Press the start button to activate the GUI. You should see the text box displaying a number that is rapidly increasing. Whenever you want the process to stop, simply press the stop button.

    GUI stopped

  10. You can resume the GUI by pressing the start button again.

Download Source Files

Click here to download the source files.

This is the end of the tutorial.