Using timer, How can I cleanly exit a function execution, when the timer fires in MATLAB?

28 ビュー (過去 30 日間)
My problem is that I can't find a Function to callback as a 'TimerFcn', that cleanly exits my function without errors.
t = timer('StartDelay',2,'TimerFcn', );
Thanks in advance.
  2 件のコメント
Guillaume
Guillaume 2019 年 12 月 13 日
Can you explain more clearly what you're trying to do exactly, particularly, the order of execution of the various things you want to happen.
taima zoabi
taima zoabi 2019 年 12 月 13 日
For Example::
function [i] = Example()
t = timer('ExecutionMode','singleShot','StartDelay',2,'TimerFcn',...
@(~,~)Command_that_exits_the_Function);
start(t)
for i=1:1000
i=i+1;
disp(i)
end
end
In this case scenario what can I use as ''Command_that_exits_the_Function'', works for me too if you have some other way to stop and exit my function when the timer fires, other than using the a callback function as 'TimerFcn'.
thank you.

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 13 日
編集済み: Walter Roberson 2019 年 12 月 21 日
There are only four ways in MATLAB to force a function to stop executing without its cooperation:
  1. quit MATLAB
  2. force an out-of-memory error
  3. force an infinite recursion
  4. Use jave robot or similar to simulate pressing control-C in the command line (note: this might not terminate immediately
There is no way to send a signal to a particular function to force that one function to stop, and there is no way to send a signal to a particular function to force that one function to error().
Therefore what you should do is write your timer to set a flag in an area that the other code checks periodically.
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 12 月 23 日
Quiting the jvm won't work if you started with -nojvm …
Walter Roberson
Walter Roberson 2021 年 3 月 3 日
I just tried in R2020b, and found that MATLAB:pmaxsize (too much memory) and MATLAB:lang:StackOverflow (too many levels of recursion) can both be caught. However, it would be worth testing out what happens when the problem is triggered in a callback that is not really part of the code being executed.

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2019 年 12 月 13 日
Why use a timer for this?
In the start of your function, start a tic
t = tic
Then periodically check
if toc(t)>2
return
end
  1 件のコメント
taima zoabi
taima zoabi 2019 年 12 月 13 日
Thank you, but I need it to exit exactly after a certain amount of time.
I didn't want to complicate things before, But what I am realy trying to do is to display images in a figure window for 30 seconds, where the image displayed changes when the user pushes the button. And after the 30 seconds are over, I want the function to exit.
So my code is:
%%%
function [] = StartTask3(Task3Time,Task3_IMG,No_of_images)
%Manual
Images_order = randperm(No_of_images);
t = timer('ExecutionMode','singleShot','StartDelay',30,'TimerFcn',...
@(~,~)Command_that_exits_the_Function);
start(t)
for i=1:40
image(eval(['Task3_IMGS.im' num2str(Images_order(i)) ';' ]));
set(gca, 'XTick', [], 'YTick', []);
w = waitforbuttonpress;
end
end
%%%

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by