How to interrupt the progrom in a GUI

Hi,
In my GUI, I try to design a push button to interrupt a current running program to stop and start a new one. A similar result can be achieved is to use Ctrl+C, but I want to stop a running program by clicking a push button in my GUI. How I can achieve this?

Answers (2)

You can call ctrl+c by using a Java Robot, but it won't do what you want. This is because the program which is running will prevent any callback from your GUI from executing until the program is over. Now what some do is put listeners into the program which specifically check the status of some variable (set by the pushbutton callback) and determine whether to keep running based on that value. For example, do this before running the program:
set(0,'userdata',0)
then in the callback to the pushbutton, put this:
set(0,'userdata',1)
then at some intervals in the program (depending on the program, this could be between a loop blocks or whatever), check the value like this:
pause(.01)
if get(0,'userdata')
return
end
You cannot do that without the cooperation of the routine being interrupted. The closest you can get without the cooperation of the routine being interrupted is to use a timer that fires repeatedly checking to see if your callback has set a flag, with the timer quitting matlab entirely when the condition is detected. Even this approach will not be able to terminate the running routine immediately if Matlab is busy executing a compiled library routine.
If you have the cooperation of the routine to be interrupted, then it can check to see if your callback has set the condition.
I might be wrong about it being necessary for the timer routine to quit matlab completely: I have seen reference to a hack involving the debugger that might be able to toss Matlab back to the command line... but still not to force a routine to quit and then start a new routine.

Categories

Asked:

on 8 Mar 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!