Daniele Kerberos
Daniele Kerberos

Reputation: 209

Stop a script in Matlab

My question is, how do I stop a script by a pressing a GUI button? I already tried to write a code that simulates "CTRL+C" press, but it doesn't work.

Upvotes: 0

Views: 3629

Answers (4)

Pursuit
Pursuit

Reputation: 12345

I don't do a lot of GUIs, but for debugging purposes I would try to set the button callback to @keyboard. That is, something like:

set(handleToGuiButton,'Callback',@keyboard)

To actually stop execution you would need to somehow communicate this button press into the loop that was executing, for example via global variables, or something fancier (e.g. https://stackoverflow.com/a/8537460/931379)

But I would honestly look at the stoploop link (from another answer) before going down any of these routes.

Upvotes: 0

siliconwafer
siliconwafer

Reputation: 732

I won't write the code for you but here's a high-level way to accomplish this:

Display a waitbar with a button on it. Create a callback function for the button which sets a flag to true.

Begin computation inside of a for-loop. In the loop: 1. update the waitbar. 2. call the drawnow function so that the callback is executed properly. Remember MATLAB is single-threaded, so this is necessary or the callback will not execute until the script finishes. 3. perform any other computation 4. check for the flag set to true. if it is true, return to stop execution.

The flag could be a global variable, or a handle-based object (so that it is passed by reference).

Upvotes: 1

oHo
oHo

Reputation: 54561

EDIT:
This answer is not applicable for the current question.
This answer is applicable only for scripts having the first line = #!/usr/bin/matlab

use pkill without option will send a TERM signal:

pkill yourscriptname

If you really want the same signal as CTRL+C then:

pkill -3 yourscriptname

If your script still does not stop, you can use the most aggressive signal KILL:

pkill -9 yourscriptname

Of course, if you known the PID (Process IDentifier), you can simply use kill:

kill    yourPID
kill -3 yourPID 
kill -9 yourPID 

You can have more info about signals using one of these commands:

man 7 signal
kill -l
info signal

Upvotes: 0

aardvarkk
aardvarkk

Reputation: 15996

I'm not sure there's a way to stop another script from being called. One alternative would be to set a global variable that's periodically checked by the script you wish to stop. If you set the value of a "stop processing" variable to true in your callback, the other script could stop if it found that it was supposed to stop.

Edit

If you'd like to have a GUI option to stop an ongoing process, I would recommend you take a look at something like STOPLOOP on the MATLAB File Exchange.

Upvotes: 1

Related Questions