Matlab GUI Tutorial - How to Stop a Long Running Function without using “control + c”
07 Dec 2007 Quan Quach 14 comments 6,573 views
Introduction
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
-
First, download the sample GUI 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. -
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);
-
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);
-
Now, save your .m file and run the GUI. You should see the following GUI appear

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

-
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.
14 Responses to “Matlab GUI Tutorial - How to Stop a Long Running Function without using “control + c””
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Thanks a lot! It was very helpful.
That can be a very useful technique. Can you give us your opinion on what would be a good way to make a generic pause/play gui that works with an external calculation?
Thanks,
-naor
I have a audio signal which sometimes taking too long to load, so i need to set a stop button just in case the user wants to stop running the program. However, if i use the method as shown above, the sound will nonstop looping in the while loop n having multiple repeat sound until i click the button. is there any way that the sound will oni produce once and also to have a stop button to stop the long prog to run (like how ctrl c function )??
hehe this is a clever little trick. thanks, this is going to be very helpfull as I will put it to use immediately.
Thank you extra very much! I was simply searching for something like ‘drawnow’.
First I’d like to say thanks, this is very useful, as is your entire site. Keep up the good work!
I have used your above approach to put “play”/”pause”/”reset” buttons on a GUI that continually updates graphical plots as data changes over time.
However, I find that unless I put a fairly large pause (pause(0.2), say) statement in the loop, the GUI does not respond to button presses - and even with the pause its necessary to repeatedly press.
I am new to Matlab. In Java I would have the button press wait on the event queue, but Matlab is single-threaded, right? So, is there a more elegant way, or am I stuck with pause?
Thanks a lot!!!
This was actually helpful not as a start/stop (I use uiwait & uiresume for that), but more so to dynamically update a ‘time’ counter as the loop moves and make it visible to the user. Thanks!
Glad it was helpful!
Thank’s a lot, this seems a simply example but this gonna rise the development of the newer user of guide & gui. This gonna help me with a PaP motor.
[...] Read input without wait from the keyboard in Matlabs main thread, here and see the related tutorial. [...]
Hi!
It’s very helpful and easy solution!
Thanks
Thanks a lot for this site
A saver website :D, easy to follow and results right away … Thanks and keep it up