Use timer to run script every ten seconds

조회 수: 40 (최근 30일)
Cary
Cary 2015년 9월 25일
댓글: Stephen23 2015년 9월 25일
I have a script that I want to execute automatically every ten seconds indefinitely until I tell it to stop. The script is called 'KelEdge'. I tried to figure out the code but I am having difficulty. Here is what I have:
function t = autoVIX()
t = timer;
t.StartFcn = @autoVIXstart;
t.TimerFcn = @runKelEdge;
t.StopFcn = @autoVIXCleanup;
t.Period = 10;
t.TasksToExecute = inf;
t.ExecutionMode = 'fixedRate';
end
function autoVIXstart(KelEdge,~)
KelEdge;
end
function runKelEdge(KelEdge,~)
KelEdge;
end
function autoVIXCleanup(KelEdge,~)
disp('Stopping KelEdge.')
delete(KelEdge)
end
Thanks in advance for all the help.
  댓글 수: 1
Stephen23
Stephen23 2015년 9월 25일
It would be much better if you used fucntions instead of scripts. Functions have many advantages over scripts, e.g. encapsulation, abstraction, their own workspaces. It makes code easier to write and test when you use functions.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 25일
t = timer;
t.Period = 10;
t.TasksToExecute = inf;
t.ExecutionMode = 'fixedRate';
t.TimerFcn = @(src, event) run('KelEdge');
start(t)
I would recommend changing KelEdge into a function instead of a script. If you have it accept two arguments, then even if it ignores the arguments you could code as
t.TimeFcn = @KelEdge;
you cannot do this for a script because you cannot create a handle to a script, only a handle to a function.
  댓글 수: 2
Cary
Cary 2015년 9월 25일
편집: Cary 2015년 9월 25일
Thanks...I don't understand the inputs to the timer function, I don't have a src or event, just a script...also when I run this, it runs infinitely i.e. it never stops and I can't stop it. What did I do wrong?
Walter Roberson
Walter Roberson 2015년 9월 25일
stop(t) to stop it.
You do have a script, but I recommend you make it a function instead.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by