Is there any way to set time limit on a function?

21 ビュー (過去 30 日間)
Mohammad Farshchin
Mohammad Farshchin 2015 年 4 月 15 日
回答済み: Jan 2015 年 4 月 16 日
I would like to stop a function if it did not finish after a specific time. Is there any way to do it in MATLAB?
  1 件のコメント
Jan
Jan 2015 年 4 月 16 日
Please add the important detail to the body of the question, that you want to stop an external program and not a Matlab function. Hiding this detail in a comment to an answer is misleading.

サインインしてコメントする。

回答 (3 件)

Sean de Wolski
Sean de Wolski 2015 年 4 月 15 日
Pass tic into the function and have it check toc periodically. If it exceeds, return or error.

Adam
Adam 2015 年 4 月 15 日
You can use tic toc.
doc tic
doc toc
Just run with a while loop, extracting the toc value into a variable and testing that it is less than your chosen time in the while loop.
You could also use a timer object, but tic toc is simpler in this case I would think.
  2 件のコメント
Mohammad Farshchin
Mohammad Farshchin 2015 年 4 月 15 日
Thank you very much for your response but the problem is that, in my function I have a line that executes an external program. So the code has to wait until the external program returns something. So it is not possible to use tic and toc.
Adam
Adam 2015 年 4 月 15 日
So you want that external program to be cancelled when the time limit is up? In that case I'm not familiar enough with calling external programs to know how to terminate them early.
If the program is interruptible then it may be possible with a timer object using a callback though I don't know what the instructions would be.

サインインしてコメントする。


Jan
Jan 2015 年 4 月 16 日
How do you want to stop the external function? Is it possible to stop it in a regular way or do you have to kill the process in the hard way? The second method might leave some partially written output files and other junk on your computer.
You can start the external function using system('Program.exe &') and wait the wanted time. The exact procedure depends on how the external function can show, that it has finished and on your operating system. So please add more details in the body of your question - posting this as a comment is not optimal for the readers.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by