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?

nightcod3r
  • 752
  • 1
  • 7
  • 26
  • 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). – Hoki Jan 18 '15 at 11:21
  • As soon as the condition is met. I tried to exemplify it. – nightcod3r Jan 18 '15 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?) – Hoki Jan 18 '15 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. – nightcod3r Jan 18 '15 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`](http://mathworks.com/help/matlab/ref/oncleanup.html). 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). – Hoki Jan 18 '15 at 13:35
  • Yep, that looks like a feasible option as well. Thx! – nightcod3r Jan 18 '15 at 14:03
  • See also http://stackoverflow.com/q/19306577/2778484 and for a lot more on setting debug points check [here](http://stackoverflow.com/a/22853963/2778484). Sorry for the shameless self promotion, but the links actually seemed fairly apropos to the question. – chappjc Jan 18 '15 at 22:08

1 Answers1

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.

Daniel
  • 36,610
  • 3
  • 36
  • 69