How to catch CTRL+C

In my previous post, I was talking about this annoyance on catching the interruption triggered by CTRL+C.

It turned out, as I suggested, that I found a work around to this issue. As I searched, I realized that it is also not a trivial thing to do in C++. It seems to be highly plateform dependent.

I have the feeling that Mathworks introduced OnCleanup expecially to deal with these issues. In contrary to TRY/CATCH, On cleanup does catch CTRL-C. The purpose of this function seems to be different though. TRY/CATCH is to deal with errors. OnCleanup is to help you cleanup your workspace in all situation (in normal operation and after errors). The advantage of such a scheme is that you don’t have to write down these clean-up operations 2 times (ie in your catch AND in your try). Let’s suppose you want to use the code I proposed to pause your GUI during calculation. Now instead of this :

% We turn the interface off for processing.
InterfaceObj=findobj(HandleToFigWindow,'Enable','on');
set(InterfaceObj,'Enable','off');

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         %
%   PUT YOUR CODE HERE    %
%                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%

% We turn back on the interface
set(InterfaceObj,'Enable','on');

I remind you that HandleToFigWindow is the handle to your GUI interface.

With onCleanup, You would do this :

% We turn the interface off for processing.
InterfaceObj=findobj(HandleToFigWindow,'Enable','on');
set(InterfaceObj,'Enable','off');

% We turn it back on in the end
Cleanup1=onCleanup(@()set(InterfaceObj,'Enable','on'));

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         %
%   PUT YOUR CODE HERE    %
%                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%

This look very similar, except that the second version will ensure that you interface is back on even if a CTRL+C has been sent by your GUI user. Notice how I removed the set in the end.

This entry was posted in Advanced, Annoyances, Intermediate. Bookmark the permalink.

3 Responses to How to catch CTRL+C

  1. Jason says:

    It doesn’t look like you ever actually turn the interfaces off in the revised code. It just looks like you find the ‘on’ objects, then have them set ‘on’ again on any cleanup. Should this not be:

    % We turn the interface off for processing.
    InterfaceObj=findobj(HandleToFigWindow,’Enable’,’on’);
    set(InterfaceObj,’Enable’,’off’);

    % We turn it back on in the end
    Cleanup1=onCleanup(@()set(InterfaceObj,’Enable’,’on’));

    %%%%%%%%%%%%%%%%%%%%%%%%%%%
    % %
    % PUT YOUR CODE HERE %
    % %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%

  2. Pieter van Vugt says:

    Instead of Cleanup1=onCleanup(@()set(InterfaceObj,’Enable’,’on’));

    you can also do
    Cleanup1=onCleanup(@()CustomCleanup(local_variables));

    and write a function (CustomCleanup) that is accessible from the location where you put the onCleanup command, with any functionality you like. Any arguments (in this example “local_variables”) that you pass should also be in the workspace of the place where onCleanup is called.

    Off course, errors occurring in this CustomCleanup function will result in two successive error reports in the command window (the red text).

Leave a Reply

Your email address will not be published. Required fields are marked *