Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:49:51+00:00 2026-06-01T15:49:51+00:00

I would like to be able to terminate my current running scripts( functions )

  • 0

I would like to be able to terminate my current running scripts( functions ) by calling a command in the code. Return would only terminate the current function not entire script. Therefore return is not the one.

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.

ultimately I want to terminate the entire running scripts upon closing a figure:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:49:52+00:00Added an answer on June 1, 2026 at 3:49 pm

    Here is a sample function with example based on yuk’s answer. Components include:

    • 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

    Sample function is below:

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to be able to set colors of headings and such, different font
I would like to be able to loop through all of the defined parameters
We would like to be able to nightly make a copy/backup/snapshot of a production
I would like to be able to use the Tab key within a text
I would like to be able to display some dynamic text at the mouse
I would like to be able to obtain all the parameter values from the
I would like to be able to define and use a custom type in
I would like to be able to predict what will be in the resulting
I would like to be able to do such things as var m1 =
I would like to be able to spawn a process in python and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.