Terminating function call with CTRL-C but keep the main function working
36 ビュー (過去 30 日間)
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!
回答 (2 件)
Jan 2020 年 12 月 30 日
編集済み: Jan 2020 年 12 月 30 日
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;
fprintf('Stopped after %d seconds.\n', etime(clock, startTime));
Now you can terminate the child window without killing the main process.
Athul Prakash 2020 年 12 月 30 日
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 ...
% 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!