2

Is there any way to stop a script launched with 'run' or 'source' from Matlab / GNU Octave? I mean different from Ctrl-C, say that a given condition (perhaps given by global variables) holds and a break signal is sent to it.

Example:

The script haltable.m is to be stopped when the environment variable takes a value higher than 0.5.

global environment

while (true)
  environment = rand;
endwhile

It is launched with

global environment

run ('haltable.m')

Where (outside of haltable.m, of course) could it be specified that it must halt after the condition is met?

7
  • 1
    Would the stop happen always in the same place in the script ? or be interrupted as soon as a "stop" condition is raised ? Is the script a major loop ? Post some example code please (make a minimal working example if your original script is too long). Commented Jan 18, 2015 at 11:21
  • As soon as the condition is met. I tried to exemplify it. Commented Jan 18, 2015 at 12:27
  • I am still not to sure to understand the purpose of this. If the condition can be checked within the loop, you can program an exit condition, if not what is the problem with ctrl+C ? (what does it do or not do wrong?) Commented Jan 18, 2015 at 13:22
  • I'm implementing a rudimentary platform for Octave programming. The scrip is programmed by the learner, so she's not suppose to know how to detect the stop condition (it must be done from the outside), and Ctrl-C would stop both, script and platform. Commented Jan 18, 2015 at 13:27
  • 1
    Sorry I really don't know how to do that in script. If you ever want to manage Ctrl+C when running function, I recommend you look at the function oncleanup. This will allow you to intercept the Ctrl+C and run some code after (not what you ask now but kind of related, specially looking at one of your older question). Commented Jan 18, 2015 at 13:35

1 Answer 1

5

It is not possible to implement such a stop condition outside the script, matlab is single threaded and nothing outside is executed. Maybe a conditional breakpoint is what you are looking for.

dbstop in haltable at 5 if (environment>.5)

You have to replace 5 with the correct line number. This does not stop the script but halts it and switches to the debugger.

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

2 Comments

Good point. I guess this is the only alternative to interact with the script.
Very clever. ;) +1 I love the db* tools.

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.