Stop and start timer in GUI with different StartDelay

12 views (last 30 days)
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.

Answers (1)

Pavel Dey
Pavel Dey on 19 Jan 2016
Edited: Pavel Dey on 19 Jan 2016
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.
  1 Comment
Bris
Bris on 20 Jan 2016
Thanks for the answer Pavel. I don't know how I missed the timerfind function when I read about timers and it is indeed good practice. I works perfectly in the example you gave. However, it still doesn't quite work when I am programming my GUI. Even though the nested function stops the timer using timerfind by the timers name, it still continues to update at the end of every period after it is started anew. I still think it might be related to the fact that it is in a nested function since I can make it work in the MATLAB command window...

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!