Stop and start timer in GUI with different StartDelay
10 次查看(过去 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!