Stop and start timer in GUI with different StartDelay
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello,
I have just begun to work with timers in GUIs and I have encountered a problem that I can't make sense of.
Problem: I have made a GUI that show some results based on some data that I would like for it to update by itself. The new data is available roughly every 15 minutes but not precisely. That is why I have tried to program a timer to check for the new data on the precise times and if the data is not there it should check every 5 seconds until the new data is loaded. When the new data is there, the timer should be set to look for updates the next whole 15 minutes by setting the StartDelay property. The code looks something like this:
function myGUI
%
%Initialization
%
%
t=timer('name','TheTimer','period',5,'timerfcn',@UpdateData,'executionmode','fixedspacing');
start(t);
%%%%%Functions%%%%%
function UpdateData(~,~)
%
%Check for update
%
if "new data is there"
stop(t);
% "Do stuff to new data"
t.StartDelay= "Seconds to the next whole 15 minutes"
start(t);
end
end
%Rest of the GUI functions
end
However, even if I stop the timer, set the new DelayStart and start it again, then it continues to run the UpdateData function. I have also tried to use the startat() function without luck. I think I might be missing something about how the timer works in connection with GUI's because I can make it work when doing it on a simple example in the command window.
Of course I could just make it update every 5 seconds. I just don't think it makes sense to search for updates when I am certain there is none.
I hope someone can tell me what I have missed. Thanks in advance.
댓글 수: 0
답변 (1개)
Pavel Dey
2016년 1월 19일
편집: Pavel Dey
2016년 1월 19일
I think the issue is not with GUI. Rather it is related to controlling the timer object. Since the timer works in background and is more closely associated with the system, it is a good practice to stop and kill a timer using 'timerfind' command.
Refer to the following code
x=12;
t0=5;
t=timer('Period' ,1,'TimerFcn',@(~,~)disp('timer running'),'ExecutionMode','fixedSpacing');
start(t);
while(x>10)
out=timerfind;
stop(out);
t.StartDelay=t0;
start(t);
end
out=timerfind;
stop(out)
delete(out)
Run this code form a script. Here x you can change from MATLAB Command Window while the timer is running. 't0' is also another variable which can be changed while running. You will observe how the 'StartDelay' parameters changes the output. You may modify your code in the above mentioned manner.
Always delete the timers after the execution is finished. Otherwise it is possible that you may not be able to control the timer once you loose the handle and it will keep running in the background.
Hope that helps. Thanks.
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!