MATLAB interrupt programmatically on Windows

When using MATLAB through the GUI, I can interrupt the calculation 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. This will only interrupt the calculation. I am looking for a solution that works on Windows.


Clarifications (seeing that the only defendant misunderstood):

I am looking for a way to abort any MATLAB calculation without controlling the MATLAB code that is being executed. I am looking for the software equivalent of pressing Ctrl - C in the MATLAB command window on Windows systems. This is for the Mathematica-MATLAB interface : I need to forward interrupts from Mathematica to MATLAB. As mentioned above, I already have a working Unix implementation; this question is about how to do this on Windows.

+4
source share
2 answers

One way would be to make the MATLAB Engine session visible before performing long calculations. Thus, if you want to interrupt execution, you simply paste the visible command window into focus and press Ctrl-C.

This can be done using engSetVisible

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

Scripts run 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 a pause, you can break it by pressing Ctrl + c in the command window:

cmd-window

+1
source

There is no direct way: all these procedures must be unwound and their workspaces are cleaned up, which can cause output handlers, etc.

The closest I can think of is to get your main routine to try / catch and then, when you want to interrupt, error () the specific line that catch, and when you find it, exit the main procedure.

0
source

Source: https://habr.com/ru/post/1481565/


All Articles