How can I abort a long task programmatically?
4 次查看(过去 30 天)
显示 更早的评论
In the application I'm working at, at some point a potentially very long task has to be executed. Essentially, I'm doing a certain number of stepwise linear regressions with a potentially large set of predictors each. Depending on the number of steps and on the possibility to exploit parallel computation, the task can take several minutes (20-30), or, in the worst cases, even hours.
For this reason, I would like to implement a way to stop the execution from end-user command (ideally through a push button).
The single linear regressions are independent and are done through a for (or parfor) loop, hence I read from other posts that there are different ways to force a for loop to stop from external interaction. My point is: if I use a drawnow command (or similar), the queue will be flushed and a hypothetical callback will be evaluated, but if the sub-task represented by a single command within the loop (in this case the stepwiselm function) takes itself a very long time (with 'very' meaning 1 hour, or maybe only 5 minutes), the drawnow command will be executed only at the end of the current iteration, i.e. potentially after quite a long time.
Is there any way (besides the classic and not so elegant Ctrl-C) to force the program to stop? The code is quite simple and, for more clarity, reads like the following:
for i=1:Nreg
% some quick instructions
regression=stepwiselm('some parameters depending on i');
randomVariable{i}=regression;
% some other quick instructions
end
Thank you very much.
0 个评论
回答(1 个)
Matt Sprague
2018-1-12
You could throw an error when the pushbutton is clicked with the "error" function to stop execution. Similar to https://www.mathworks.com/matlabcentral/answers/105999-how-to-stop-a-function
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!