0

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.

3
  • Sorry -- why don't you just hit Ctrl-C? Why are you trying to simulate it? Commented Feb 2, 2012 at 18:25
  • i just want a user to be able to stop the script that he started by pressing a button on a IDE. Woudl be nice to have a Cancel button. Commented Feb 2, 2012 at 18:41
  • I've adjust my answer accordingly. Commented Feb 2, 2012 at 18:43

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

8 Comments

Isn't this assuming the OP is using some Unix-based OS?
That will kill Matlab, instead of stopping the script
Yep @aardvarkk you are right. When I read CTRL+C, I thought about terminal, so Unix-based OS... Maybe I am wrong...
Hi @Andrey. You may have right. I was thinking Daniele Kerberos uses matlab scripts as gnuplot scripts. Thanks for your comment, I edit my answer ;-)
@oHessling Don't worry about it! We're all just trying to help.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.