3
votes

I'm looking to limit the execution of function in Matlab, so if it won't return answer in X seconds the call will be aborted.
I know it's possible with the 2011 Matlab version using timeout, but I've got the 2010 version of Matlab. Is it still possible to limit the execution time of the function?

1
@Alex: not exactly, the problem isn't the loops inside the function but rather the calls to external dlls that may take time. That's why I need general method for limiting .m function execution time. - shahar_m
Yes, just helping the linky goodness. - Alex
To the best of my knowledge, this is not possible. - Jonas
I don't see a timeout command in the documentation for Matlab (mathworks.com/help/techdoc/ref/funcalpha.html#T). It might be a special library. Try finding out the name of the package by using help timeout on your 2011 copy, and see if that package is available for 2010. - PearsonArtPhoto

1 Answers

3
votes

As others have pointed out you can't do this natively in Matlab. However on Unix systems e.g. Linux or Solaris I have previously used a bit of a dirty hack to achieve the desired effect.

Rather than calling your .m file as a function with parameters, save all the parameter data into a .mat file and write a shell command to invoke Matlab and run your .m file e.g. myfunc.m as a standalone routine e.g.

!bash -c "ulimit -t 3;matlab -nodisplay < myfunc.m"

This would limit myfunc.m to a CPU execution time of 3 second. Note that is CPU not including any disk access etc. There are other flags you can pass to ulimit if you need some other behaviour.

Inside myfunc.m you'd have to save the data myfunc.m wants to return into a .mat file and load it up again in your calling program. Bit of a nasty hack but I have tested it and it works. Note the use of the bash shell for the internal ulimit command.