Matlab:实现CTRL + C的功能,但在代码中

时间:2012-04-05 17:21:02

标签: matlab

我希望能够通过调用代码中的命令来终止当前运行的脚本(函数)。返回只会终止当前函数而不是整个脚本。因此,回归不是那个。

我正在寻找的是一个完全符合 CTRL + C 的命令。 我已经看到了这一点:how to stop execution 并注意到还没有人为这个问题提供正确的答案。

最终我希望在关闭数字时终止整个正在运行的脚本

hFig = figure('CloseRequestFcn',{@closeHandler});

.
.
.
function closeHandler (src,evnt)

    CTRL+C    <--- I am looking for such a command     
end

PS。 功能错误()也不起作用:试试这个:

function terminateInCode()

hFig = figure('CloseRequestFcn',{@closeHandler});

while(1)

   plot(10*rand,10*rand,'+');
   pause(0.1);
end;

   function closeHandler (src,evnt)
      delete(hFig);
      error('program terminated!');
   end
end

7 个答案:

答案 0 :(得分:12)

以下是一个示例函数,其示例基于yuk's answer。组件包括:

  • 确保命令窗口具有焦点以接收 CTRL + C
  • 使用计时器在发生中断后释放 CTRL + C
  • 使用Java机器人按 CTRL + C

示例功能如下:

function terminateExecution
%terminateExecution  Emulates CTRL-C
%    terminateExecution   Stops operation of a program by emulating a
%    CTRL-C press by the user.
%
%    Running this function
%
%Example:
%for ix = 1:100
%    disp(ix)
%    if ix>20
%        terminateExecution;
%    end
%end

%1) request focus be transferred to the command window
%   (H/T http://undocumentedmatlab.com/blog/changing-matlab-command-window-colors/)
cmdWindow = com.mathworks.mde.cmdwin.CmdWin.getInstance();
cmdWindow.grabFocus();

%2) Wait for focus transfer to complete (up to 2 seconds)
focustransferTimer = tic;
while ~cmdWindow.isFocusOwner
    pause(0.1);  %Pause some small interval
    if (toc(focustransferTimer) > 2)
        error('Error transferring focus for CTRL+C press.')
    end
end

%3) Use Java robot to execute a CTRL+C in the (now focused) command window.

%3.1)  Setup a timer to relase CTRL + C in 1 second
%  Try to reuse an existing timer if possible (this would be a holdover
%  from a previous execution)
t_all = timerfindall;
releaseTimer = [];
ix_timer = 1;
while isempty(releaseTimer) && (ix_timer<= length(t_all))
    if isequal(t_all(ix_timer).TimerFcn, @releaseCtrl_C)
        releaseTimer = t_all(ix_timer);
    end
    ix_timer = ix_timer+1;
end
if isempty(releaseTimer)
    releaseTimer = timer;
    releaseTimer.TimerFcn = @releaseCtrl_C;
end
releaseTimer.StartDelay = 1;
start(releaseTimer);

%3.2)  Press press CTRL+C
pressCtrl_C

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pressCtrl_C
    import java.awt.Robot;
    import java.awt.event.*;
    SimKey=Robot;
    SimKey.keyPress(KeyEvent.VK_CONTROL);
    SimKey.keyPress(KeyEvent.VK_C);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function releaseCtrl_C(ignore1, ignore2)
    import java.awt.Robot;
    import java.awt.event.*;
    SimKey=Robot;
    SimKey.keyRelease(KeyEvent.VK_CONTROL);
    SimKey.keyRelease(KeyEvent.VK_C);

答案 1 :(得分:7)

不确定它会起作用,只是一个想法。如何用MATLAB模拟键盘按键?

您可以尝试java.awd.Robot

import java.awt.Robot;
import java.awt.event.*;
SimKey=Robot;
SimKey.keyPress(KeyEvent.VK_CONTROL);
SimKey.keyPress(KeyEvent.VK_C);

WScript.Shell and SendKeys

答案 2 :(得分:6)

不幸的是,它似乎无法完成:

Mathworks

  

除了使用键盘的之外,无法以编程方式在MATLAB中发出 Ctrl + C > Ctrl + C 组合。

     

作为替代方法,您可以使用ERROR命令强制执行将退出代码的错误。例如:

error('Program terminated for a specific reason')

答案 3 :(得分:3)

这是一种使用未记录的Matlab调用将键事件直接放入命令窗口的替代方法。这样做的方法受到保护;这使用反射去保护它。

与@yuk和@ Persuit的答案不同,这似乎没有控制键粘滞的问题。此外,它将始终直接发布到命令窗口,没有任何竞争条件或其他确保焦点的问题。并且,我认为它会确定地触发 - 它会立即执行。

一个警告是它使用未记录的调用来检索命令窗口实例的句柄。由于取决于窗框布局,因此会因释放而略有不同。 Yair Altman(undocumentedmatlab.com)在文件交换方面的一些工作具有更强大的功能,可以以更一般的方式获取它;这段代码应该适用于大多数现代版本的Matlab(在R2011a上测试,包括Mac和Win)。

function interrupt

import java.awt.event.KeyEvent
import java.util.Calendar
import java.lang.reflection.*

cmdwin = handle(com.mathworks.mde.cmdwin.CmdWin.getInstance().getComponent(0).getComponent(0).getComponent(0),'CallbackProperties');

argSig = javaArray('java.lang.Class',1);
argSig(1) = java.lang.Class.forName('java.awt.event.KeyEvent');
method = cmdwin.getClass().getDeclaredMethod('processKeyEvent',argSig);
method.setAccessible(true);

cal = Calendar.getInstance();
args = javaArray('java.lang.Object',1);
args(1) = KeyEvent(cmdwin,KeyEvent.KEY_PRESSED,cal.getTime().getTime(),KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_C,KeyEvent.CHAR_UNDEFINED);
method.invoke(cmdwin,args);

答案 4 :(得分:2)

您可以使用error功能。你将回到matlab。

它会产生错误,但这也是在matlab脚本中按CTRL+C时通常会发生的错误。

您应该添加某种类型的消息,例如error('Interrupted by user');

答案 5 :(得分:1)

这不完全是你要求的,但考虑到你的例子,你的问题可以像这样解决:

function terminateInCode()

hFig = figure('CloseRequestFcn',{@closeHandler});

stop=0;
while(~stop)
   plot(10*rand,10*rand,'+');
   pause(0.1);
end;

   function closeHandler (src,evnt)
      delete(hFig);
      stop=1;
   end
end

答案 6 :(得分:-2)

尝试使用return语句。它会让你退出一个功能。

如果要完全终止它,则需要使用ERROR。

如果它确实是灾难性的,你总是可以使用EXIT。