How to run a for loop every one second ?

In M Script how to execute a for loop every one second?

3 件のコメント

per isakson
per isakson 2013 年 5 月 9 日
Did you consider to use the timer?
doc timer
Arun Badigannavar
Arun Badigannavar 2013 年 5 月 10 日
ya but i am unable to do it without pause command
sharad kamble
sharad kamble 2016 年 10 月 7 日
Thank you for the solution. .

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

 採用された回答

Friedrich
Friedrich 2013 年 5 月 10 日
編集済み: Friedrich 2013 年 5 月 10 日

2 投票

Try this,
function a = test
a = timer('ExecutionMode','fixedRate','Period',1,'TimerFcn',@myfun);
start(a);
end
function myfun(obj,evt)
for i=1:3
disp(datestr(now));
end
disp('===============');
end
and call it with
my_timer = test();
When you like to stop the timer call
stop(my_timer)
Or in the case you want to wait until the timer is done and you know how often the for loop should be triggered do this:
function a = test
a = timer('ExecutionMode','fixedRate','Period',1,'TimerFcn',@myfun,'TasksToExecute',10);
start(a);
wait(a);
disp('timer done')
end
function myfun(obj,evt)
for i=1:3
disp(datestr(now));
end
disp('===============');
end
Make sure the tme myfun needs to run is lower than 1 second.

その他の回答 (0 件)

カテゴリ

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by