Timer object is only executing part of the timerfcn on second and later iterations.

5 views (last 30 days)
I am using a timer object coupled with a push button in a GUI in order to periodically update GUI text every 30 seconds as well as read in data that is being produced while the GUI is running in a file directory. The text updating the current time/last update is working properly, however after the first iteration, the functions that are reading in the data are not running. I know this is the case because in various debug attempts I ran t.AveragePeriod which returned a NaN value. Here's the effective code I have thus far:
Note that I've tried multiple different versions of executionmode and it fails every time.
I didn't originally have the t.BusyMode = 'queue' line in at first but it didn't seem to change anything.
t = timer;
t.ExecutionMode = 'fixedSpacing';
t.TasksToExecute = 100;
t.Period = 30;
t.BusyMode = 'queue';
t.StartDelay = 0;
t.StartFcn = @(~,thisEvent)disp('Program execution started.');
t.TimerFcn = @(~,thisEvent)[set(h.currentTime,'String',currentTime()),readDataFieldA(f),readDataFieldB(f)];
t.StopFcn = @(~,thisEvent)[set(h.currentTime,'String','Test Complete.'),disp(num2str(t.AveragePeriod))];
t.ErrorFcn = @(~,thisEvent)disp('Error encountered. Debug.');
After the first iteration, and the data is read, I will get printed out the statement "Program execution started" as if the timer is starting all over again. It is to my understanding that this startfcn function should only call one time when the timer starts. It seem weird to me that the updating of the current time text on the GUI is working using the timer function every 30 seconds but the remaining readDataField functions are not.
  1 Comment
Jan
Jan on 22 Jan 2019
Please post a minimal working example. What is "h.currentTime"? What is currentTime()? I guess wildly, that this starts a new timer object. What does readDataFieldA(f) and readDataFieldB(f) do?

Sign in to comment.

Answers (1)

Jan
Jan on 22 Jan 2019
If you use function instead of anonymous functions, you can use the debugger easily to see, what's going on.
t.TimerFcn = {@myTimerFcn, h, f};
...
function myTimerFcn(TimerH, EventData, h, f)
t = currentTime();
set(h.currentTime, 'String', t)
readDataFieldA(f);
readDataFieldB(f);
end
Now setting a breakpoint into the callback will clarify what's going on. Why using code, which is such compact that you cannot read or debug it, when you can have a clean and clear function?
  1 Comment
Eric Reinecke
Eric Reinecke on 22 Jan 2019
Using code like this because I'm new to using the timer object and this is what the matlab documentation gave as an example, I followed it straight from that. I have little to no idea how to actually implement a timer other than what they gave in the documentation. So essentially, this is not my code and I'm only trying to adapt it. h.currentTime is the text field in the gui with format "Last update: 10:28:39" i.e. hours minutes seconds format.
currentTime(); is a function that converts the current time using clock to the format specified above, a small function that I ran that needs to be called to update the GUI text.
readDataFieldA(f) and readDataFieldB(f) read in data. Hardware is being tested and generating data into a file directory which is continuously being updated. These data files are binary files and the functions readDataField(A) and readDataFieldB(f) read the binary data in, and using the timer function above, should in tandem be checking if the files are updated and reading in the EXTRA data that has come in since the last read from the most recent file (sometimes new data files are created.)
I don't need help on those functions, they are working properly. I just don't have any experience using a timer function and need help with that functionality. I will try your method of using a cleaner function to debug.

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!