4

When using MATLAB through the GUI, I can interrupt a computation by pressing Ctrl-C.

Is there a way to do the same programmatically when using MATLAB through the MATLAB Engine C API?

On Unix systems there is a solution: send a SIGINT signal. This will not kill MATLAB. It'll only interrupt the computation. I am looking for a solution that works on Windows.


Clarifications (seeing that the only answerer misunderstood):

I am looking for a way to interrupt any MATLAB calculation, without having control over the MATLAB code that is being run. I'm looking for the programmatic equivalent of pressing Ctrl-C in at the MATLAB command window, on Windows systems. This is for a Mathematica-MATLAB interface: I need to forward interrupts from Mathematica to MATLAB. As mentioned above, I already have a working implementation on Unix; this question is about how to do it on Windows.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • [Cross post on MATLAB Answers](http://www.mathworks.com/matlabcentral/answers/76405-programmatically-interrupt-matlab-on-windows) – Szabolcs May 20 '13 at 14:52

2 Answers2

1

One way would be to make the MATLAB Engine session visible, prior to executing long computations. That way if you want to interrupt execution, you just bring the visible command window into focus and hit Ctrl-C.

This can be done using the engSetVisible function

Here is a quick example I tried using MATLAB COM Automation. The process should be similar since MATLAB Engine is implemented using COM on Windows (pipes are used on Unix instead).

The scripting is done in Powershell:

# create MATLAB automation server
$m = New-Object -ComObject matlab.application
$m | Get-Member

# make the command window visible
$m.Visible = $true

# execute some long computation: pause(10)
$m.Feval('disp', 0,[ref]$null, 'Press Ctrl-C to interrupt...')
$m.Feval('pause', 0,[ref]$null, 10)

# close and cleanup
$m.Quit()
$m = $null
Remove-Variable m

During the pause, you can break it by hitting Ctrl+c in the command window:

cmd-window

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Interesting! I thought I tried this and it didn't work. Let me try it again. – Szabolcs May 20 '13 at 21:45
  • 1
    It turns out that if you use `Feval`, then the output goes to the command windows and Ctrl-C works. If you use `Execute`, Ctrl-C in the command window does not work. – Szabolcs May 20 '13 at 21:55
  • @Szabolcs: weird indeed. Truth is for some reason the `Execute` method is not exposed when using Powershell... I tried Python this time and it behaves as you describe, Ctrl-C only works with `Feval`: http://pastebin.com/kQSW6E5v – Amro May 20 '13 at 22:17
  • @Szabolcs: can you also test this with an actual program linked against MATLAB Engine? – Amro May 20 '13 at 22:24
  • I have tried with an actual Engine program. Ctrl-C does nothing for evaluations started by the Engine and it crashes MATLAB for evaluations started in the command window. `Execute` is exposed for me with PowerShell, and it works correctly. – Szabolcs May 20 '13 at 22:36
  • I think I found a solution for MEX (not the engine): the `utSetInterruptPending(bool)` function from `libut.dll` seems to work for interrupting. At least it works when loaded using `libraryload` and evaluated sequentially. I'll need to evaluate it from a second thread while a MATLAB evaluation is working. But I hope it will work, and we can use it once we switch to MEX. – Szabolcs May 20 '13 at 22:38
0

There isn't a direct way: all of those routines have to be unwound and their workspaces cleaned up, which might invoke exit handlers, and so on.

The closest I can think of is to have your main routine have a try/catch and then when you wish to abort, error() the particular string that the catch is keyed for, and when you detect it, bail out cleanly from your main routine.

  • I'm afraid you misunderstood. I don't have control over the code that is being run in MATLAB. To simplify, imagine that you are writing your own GUI for MATLAB, and you're using the Engine API. With the official GUI you can stop computations using Control-C. Any useful GUI should have that so you want to make this possible for your own too. How? (My situation is different but analogous.) – Szabolcs May 19 '13 at 14:26