Running a timer inside another timer
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, so I need to run a timer inside another timer, and I need to keep the properties of each timer specific to THAT timer. I was able to get it to run the inside timer once, but it never repeated once it went to the outside timer. Seems like there is an issue with the outside timer. May I please have some assistance with this by any ideas on how to go about it? Thank you.
댓글 수: 6
Adam Danz
2023년 1월 12일
편집: Adam Danz
2023년 1월 12일
This isn't a minimal working example. If you could provide a watered down version with the bare minimum components that we could copy-paste-run, that would expedite the troubleshooting process.
Where are you starting these timers? I see thier start functions but I don't see thier start commands.
답변 (1개)
Adam Danz
2023년 1월 14일
The problem seems to be that the line that starts deprivTimer uses "app.deprivTimer" but "app" is not one of the input arguments in that function.
function CHATstep (cTimer, event) %<---------- "app" is not an input
event_type = event.Type;
switch event_type
case 'StartFcn'
disp([event_type ' dayTimer started ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
case 'TimerFcn'
disp([event.Type ' dayTimer executed ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
start(app.deprivTimer); % <--------------- HERE
case 'StopFcn'
disp([event.Type ' dayTimer Fcn complete ',datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
stop(cTimer);
end
end
To fix that, you need to include the app object in the function definitions. Here are examples you can apply to your file.
app.dayTimer.StartFcn = @(timer,event)CHATstep(app,timer,event);
app.dayTimer.TimerFcn = @(timer,event)CHATstep(app,timer,event);
app.dayTimer.StopFcn = @(timer,event)CHATstep(app,timer,event);
function CHATstep (app,cTimer, event) % <-- add app input
...
start(app.deprivTimer); % <--- now this will work
...
end
However there are some other puzzles here. First it appears that there is much time between events. This line below suggests several minutes. The other section below shows that you're pausing for potentially long periods. Make sure you reduces these to short periods of time when testing and developing.
app.dayTimer.Period = app.waitTime * 3600
case 'TimerFcn'
pause(app.deprivTimer.UserData(2)) % pauses for the specified random interval until the deprivation movement should start
pause(mTimer.UserData(1)) % pauses the function until the deprivator is finished cycling
Lastly, as mentioned in the documentation for timer, timers like these should not be used for precise control of temporal events. So if your program requires precise timing control (sub-second), be aware that of these limitations.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!