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.
Here's one way to do it, by periodically checking the value of a UI toggle - which says Continue/Interrupt.
function FunctionThatTakesTooMuchTime
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';
lastTocValue = 0;
tic;
if(toc > lastTocValue+10)
lastTocValue=toc;
if(tb2.Value==1)
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
1 Comment
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/703357-terminating-function-call-with-ctrl-c-but-keep-the-main-function-working#comment_1231282
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/703357-terminating-function-call-with-ctrl-c-but-keep-the-main-function-working#comment_1231282
Sign in to comment.