Effacer les filtres
Effacer les filtres

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

23 vues (au cours des 30 derniers jours)
Adrián García
Adrián García le 27 Déc 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 commentaire
Mario Malic
Mario Malic le 28 Déc 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.

Connectez-vous pour commenter.

Réponses (2)

Jan le 30 Déc 2020
Modifié(e) : Jan le 30 Déc 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
exitCode = proc.exitValue();
fprintf('Exited after %d seconds.\n', etime(clock, startTime));
running = false;
if running
fprintf('Stopped after %d seconds.\n', etime(clock, startTime));
Now you can terminate the child window without killing the main process.

Athul Prakash
Athul Prakash le 30 Déc 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;
% 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)
if(tb2.Value==1) % Check whether the 'Interrupt' button has been toggled.
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!


En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by