MATLAB Answers

Terminating function call with CTRL-C but keep the main function working

35 views (last 30 days)
Adrián García
Adrián García on 27 Dec 2020
Answered: Athul Prakash on 30 Dec 2020
I need to test some functions for efficiency. For that, I have one function that runs them all and registers the amount of time taken to complete them.
However, some functions take WAY too much time, so I don't need anymore the exact value (I just know they are way above the average). I would like to be able to cancell its execution with CTRL-C (or something similar), but that terminates as well the main function.
I could just write the function to terminate subcalls' executions after a fixed amount of time (any ideas would be appreciated!), but I would like to have the ability to choose on execution, since that time might just be subjective.
Thank you in advance!

  1 Comment

Mario Malic
Mario Malic on 28 Dec 2020
What about the testing flag as an input argument, if you want to do the testing, skip the calculation part and return the value, if there's anything to return.

Sign in to comment.

Answers (2)

Jan
Jan on 30 Dec 2020
Edited: Jan on 30 Dec 2020
What about starting another Matlab instance? Then you can kill it by taskkill of the operating system (assuming you are running Windows).
Or do it independently from the platform:
% non-blocking call:
process = runtime.exec('matlab -nodisplay -nosplash -nodesktop -r "YourFcn; exit"');
startTime = clock;
running = true;
while etime(clock, startTime) < 100
pause(1);
try
exitCode = proc.exitValue();
fprintf('Exited after %d seconds.\n', etime(clock, startTime));
running = false;
catch
fprintf('.');
end
end
if running
fprintf('Stopped after %d seconds.\n', etime(clock, startTime));
process.destroy();
end
Now you can terminate the child window without killing the main process.

  0 Comments

Sign in to comment.


Athul Prakash
Athul Prakash on 30 Dec 2020
Hi Adrian,
I can't think of a straightforward solution, since MATLAB doesn't allow you to programmatically trigger a Ctrl+C ; nor can you catch a Ctrl+C keypress using try..catch in your code.
However, I think various workarounds are possible for your use case. You may get some ideas by exploring timer object as well as simulating key presses using Java Robot.
Here's one way to do it, by periodically checking the value of a UI toggle - which says Continue/Interrupt.
% If you have a function that needs to be measured ...
function FunctionThatTakesTooMuchTime
% Create a UI with Continue and Interrupt toggle buttons.
fig = uifigure('Position',[680 678 398 271]);
bg = uibuttongroup(fig,'Position',[137 113 123 85]);
tb1 = uitogglebutton(bg,'Position',[10 50 100 22]);
tb2 = uitogglebutton(bg,'Position',[10 28 100 22]);
tb1.Text = 'Continue';
tb2.Text = 'Interrupt';
% Start a counter to measure time with.
lastTocValue = 0;
tic;
%%%%%%%%%%%%%
% Normal Function Code goes here
%%%%%%%%%%%%%
% Code to check the UI toggle status if 10 seconds have passed.
% I would add this if-body multiple times in this function - inside any loop, as well as periodically after time-heavy portions of code.
if(toc > lastTocValue+10)
lastTocValue=toc;
if(tb2.Value==1) % Check whether the 'Interrupt' button has been toggled.
return;
end
end
end
If you run this, a UI dialog should pop up when the function starts. If you want the function to be interrupted, just set the toggle in the window to 'Interrupt' and when execution gets to the if-condition shown above, the function would return if 10 seconds have passed.
If embedding extra code into the function is too clunky, you may try using a timer object to routinely check the value as well every 'T' seconds.
Hope it helps!
Athul

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by