English 中文(简体)
Matlab: 执行CTRL+C的工作,但在法典中
原标题:Matlab: implementing what CTRL+C does, but in the code
  • 时间:2012-04-05 17:21:01
  •  标签:
  • matlab

我愿通过在法典中发出指挥命令,结束我目前的手稿(职能)。 返回只能终止目前的职能,而不是整个文字。 因此,回返不是。

What I am looking for is a command which does exactly what CTRL + C do. I have already seen this: how to stop execution and noticed that no one has yet provided a proper answer for this question in there either.

最后,我想在数字截止时终止整个行文:

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

.
.
.
function closeHandler (src,evnt)

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

PS. function error() will not work either: Try this:

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
最佳回答

这里是一个样本功能,例如基于。 页: 1 构成部分包括:

  • Insure the command window has focus to receive the CTRL+C
  • Use a timer to release CTRL+C after the break has occurred
  • Use a Java robot to press 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);
问题回答

不幸的是,似乎无法做到:

Mathworks

不可能在方案上发布<><>>>><>>>>。 Ctrl+C in MATLAB, supplemented using the key板 s Ctrl+>.<>

作为一种替代办法,你可以使用ERROR的指挥力来强行推卸该守则的错误。 例如:

error( Program terminated for a specific reason )

在此,使用无证件的Matlab的替代办法要求将关键事件直接置于指挥窗口。 这样做的方法是保护的;这是为了不加以保护。

与“@yuk”和“@Perpons”的答复不同,这似乎与控制的关键标签没有关系。 此外,在没有任何种族条件或其他确保重点的问题的情况下,它总是直接站到指挥窗口。 而且,我认为,这起火是决定性的——它立即执行。

The one caveat is that it uses an undocumented call to retreive the handle of the command window instance. This varies slightly by release as it is dependent upon the window frame layout. Some of Yair Altman s (undocumentedmatlab.com) work on the file exchange has more robust functions to grab this in a more general fashion; this code should work with most modern releases of Matlab (Tested on R2011a, both 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);

您可使用<代码>error。 你们将回去做事。

It will produce an error, but that is also what usually happen when you press CTRL+C, in a matlab script.

您应加上诸如<代码>ror(>)等某种信息。 由用户分类;

这不是你要求什么,而是考虑你的榜样,你的问题可以像以下那样解决:

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

询问返回情况。 它将把你推卸职能。

如果你想完全终止,你需要使用ERROR。

如果实际如此灾难性的话,你就能够永远使用EXIT。





相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签