How can I abort a long task programmatically?

1 次查看(过去 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.

回答(1 个)

Matt Sprague
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
  1 个评论
andreadr
andreadr 2018-1-13
Hi Matt,
Thanks for the reply. I've already read posts about throwing errors and tried something like
function stopExecutionButton_Callback(src,event)
error('Program interrupted')
end
The error is thrown, the message is displayed, but the running program continues its execution. The error actually stops the execution of the button callback only (in fact, potential code within the callback following the error command is not executed), but the already running function is not affected at all.
I think the point is that the running program contains a function, stepwiselm, which takes itself a very long time to execute. It would be easy to flush the queue and make a stop callback to be executed (for example using drawnow), if I could directly modify this function. Unfortunately, it is part of the Statistics and Machine Learning Toolbox, so I would have to copy/modify MathWorks stuff. Which I wouldn't really like to do (and probably I couldn't do, I guess, in particular since the application will be distributed).
What I'm probably looking for is some workaround to perform an action analogous to Ctrl-C.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by