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

C g*_*ics 21 matlab

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

我正在寻找的是一个完全符合CTRL+ C做的命令.我已经看到了这个:如何停止执行,并注意到还没有人为这个问题提供了正确的答案.

最终我想在关闭数字时终止整个运行的脚本:

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

.
.
.
function closeHandler (src,evnt)

    CTRL+C    <--- I am looking for such a command     
end
Run Code Online (Sandbox Code Playgroud)

PS.函数error()也不起作用:试试这个:

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
Run Code Online (Sandbox Code Playgroud)

Pur*_*uit 12

这是一个示例函数,其示例基于yuk的答案.组件包括:

  • 确保命令窗口具有焦点以接收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);
Run Code Online (Sandbox Code Playgroud)

  • 看起来最初发布的函数仅在命令窗口具有焦点时才有效.否则,CTRL + C将被发送到一个不会停止执行的窗口,请参阅编辑以进行修复."keyRelease"函数并不总是执行,所以我有时也会让CTRL键卡住.尚不确定是否已经解决了这个问题. (2认同)
  • @Persuit--干得好!看看我的替代方案有点简单,可以避免所有这些问题......但它是通过受保护的方法实现的.:) (2认同)

yuk*_*yuk 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);
Run Code Online (Sandbox Code Playgroud)

WScript.Shell和SendKeys.


Sma*_*ash 6

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

Mathworks公司

有没有以编程方式发出Ctrl+C在MATLAB除了使用键盘的Ctrl+C组合.

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

error('Program terminated for a specific reason')
Run Code Online (Sandbox Code Playgroud)