Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Matlab timeouts

614 views
Skip to first unread message

Dustin

unread,
Mar 27, 2014, 2:59:10 AM3/27/14
to
Hi,

I am trying to time out a MATLAB function that may or may not take too long to execute. I am able to use timers to throw errors, but they do not appear to be in a form that can be caught within a try-catch block. As an example, the code that I am using is:

function tmptimer(time_out)

t = timer('TimerFcn', 'error(''timeout'')', 'StartDelay', time_out);
start(t);

try
input('Waiting to timeout'); % Example. Can be an arbitrary function
catch
fprintf('Caught the error');
delete(t);
end

end

The output by running something like tmptimer(1) is:

Waiting to timeout??? Error while evaluating TimerFcn for timer 'timer-49'

timeout

However, the error does not stop execution! In other words, the command window still keeps waiting for the input in this particular case. Only when I do a Ctrl+C, do I get:

??? Operation terminated by user during ==> tmptimer at 7

Can anyone tell me how I can generate an error that can be picked up by a try-catch block or if there is any other way at all to time out an arbitrary function call?

Thanks

Steven Lord

unread,
Mar 27, 2014, 9:35:39 AM3/27/14
to

"Dustin " <gol...@mailinator.com> wrote in message
news:lh0i7u$sfr$1...@newscl01ah.mathworks.com...
In general? No, I don't believe there is a way to time out an arbitrary
function call (short of killing MATLAB or _maybe_ on Linux sending SIGINT to
the MATLAB process. I haven't tried the latter, but I have done the former.)

For specific functions, you could have the TimerFcn of a timer set some
application data (SETAPPDATA) that the function queries (with GETAPPDATA)
periodically; if the application data says to stop, the function could then
stop.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Dustin

unread,
Mar 27, 2014, 10:08:08 AM3/27/14
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <lh19fb$p8m$1...@newscl01ah.mathworks.com>...
>
> In general? No, I don't believe there is a way to time out an arbitrary
> function call (short of killing MATLAB or _maybe_ on Linux sending SIGINT to
> the MATLAB process. I haven't tried the latter, but I have done the former.)
>
> For specific functions, you could have the TimerFcn of a timer set some
> application data (SETAPPDATA) that the function queries (with GETAPPDATA)
> periodically; if the application data says to stop, the function could then
> stop.
>

Thanks, Steve. The app data idea is good, except that I am worried about getting stuck in certain subfunctions of the main function (working with videos, so can take some time depending on video size) which I do not control and hence, would not be able to query the app data.

I spent a few hours working on this, but in the end went with a supervisor in Linux that restarts the process if it doesn't respond with the output of the function within a set amount of time. It's better than killing from within MATLAB I think, because in extreme CPU cases like a blank "while true" loop, MATLAB seems to be blocking TimerFcn from executing until the loop is somehow exited.

Alexey

unread,
Mar 27, 2014, 11:47:08 AM3/27/14
to
To terminate an arbitrary function you need to run it as a separate thread (a process), that the main process can monitor and if necessary terminate the thread. I have no idea if it is possible in Matlab.

A possible way could be to implement the termination within you function.
To do this you can use an event function.

I just answered a similar question here
http://www.mathworks.de/matlabcentral/newsreader/view_thread/119565

Best
Alex

"Dustin" wrote in message <lh1bc8$1m9$1...@newscl01ah.mathworks.com>...
0 new messages