How can I stop a function from main function after a certain time?

% main function
callpause(200)
I have two programs. The first one (main) calls the function 'callpause'. My aim is to terminate 'callpause' from the main if it exceeds 10 seconds. (It will.) How can I do that with making changes only in the main function?
function callpause(a)
for i=1:a
pause(1)
i
end
end

1 件のコメント

KSSV
KSSV 2017 年 1 月 31 日
I don't think it is possible to achieve in main function...but can be made changes to function callpause and exit the function.

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

回答 (1 件)

Jan
Jan 2017 年 1 月 31 日

0 投票

You can use this tool to emulate a Ctrl-C after some time: FEX: Timeout. This would stop the complete program, but you could restart it using a timer also.
This kind of meta-programming is rather confusing and prone to errors: programs, which terminate and restart themselves. A structure program would be nicer and much cleaner: If the called function contains a loop, you can insert a check for the elapsed time, such that the subfunction can terminate itself and cleanup open files, persistent memory or whatever cleanly.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2017 年 1 月 31 日

回答済み:

Jan
2017 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by