Guidata not updating handles in timer stop function

4 views (last 30 days)
I created a simple audio player that simultaneously captures data from a USB device during playback via a timer object. The timer is initialized during play and stopped when paused. Data from the device is captured every second with the timer's timer function and is saved to the handles structure. However, when the timer is stopped, I want the timer's stop function to notate the amount of elapsed time (using the toc function) and save this in a marker within the handles structure.
function SacredRNG_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject; = []; % creates an empty bed for the data.
handles.Markers = [];
%%Sets Default Parameters for RNG
handles.tmr = timer('TimerFcn',{@FieldRNG_callback,hObject},... % Creates timer object as default Field RNG
guidata(hObject, handles);
Here is the timer function which works fine:
function FieldRNG_callback(obj,event,hObject) % Captures data from a connected USB based on timer
portObj = handles.portObj; % calls the serial port
ReadRate = handles.freqSam;%captures the read rate (i.e. dimensions of the numbers to be pulled from the RNG);
newdata = fread(portObj,ReadRate,'int8'); % pulls n data points each second when called by the timer = [; newdata]; % updates the data in handles with the new data captured from the device.
guidata(hObject, handles);
Here is the timer's stop function which works but does not actually update the handles structure when guidata is executed. I tested this in debugging mode and the markers are created, however, they are just not saved.
function StopRecording_callback(obj,event,hObject)
handles.Markers(end+1) = toc; %Note tic is set when playback starts
handles.MrkrLabels{end+1,1} = ['stopped @ ',num2str(round(toc)),' seconds'];
guidata(hObject, handles);
I can used the disp(handles.Markers) command and verify that the markers are created properly and appended to the existing Markers and Marker labels. However, they are not saved. My understanding was that guidata should update the current handles structure and save these values. Notably this is working in the timer function so I don't understand why it wouldn't work in the stop function. Thanks for your help!
Saurabh Gupta
Saurabh Gupta on 1 Aug 2017
It looks like you have debugged "start to pause" and have determined the data is lost during "unpause". How is this "unpause" functionality written? Have you tried debugging it to see if you are accidentally clearing the data there?

Sign in to comment.

Accepted Answer

Akhilesh Thakur
Akhilesh Thakur on 31 Jul 2017
Okay the code I see here, there are possibilities why your handles are not stored. First thing the placement of guidata is fine but did you check which hObject it is pointing to? You should set a breakpoint and check whether the hObject is the same at all other functions you are pointing to. Another thing is check the sequence of the program is the location where the guidata is updated and you are trying to get a grab of it, will it hold the handles or pinpoint to new set. If you are pointing to wrong hobject use this command get(hObject,'parent'); . If you want to keep handles structure global and available all across your program use of setappdata(0,..) would be efficient. I hope this helps.
Daemonic on 1 Aug 2017
Perfect. Yes that appeared to be the issue. Thank you!

Sign in to comment.

More Answers (1)

Jan on 1 Aug 2017
I assume that there is a mistake in your way to check the success of the update of the handles struct. You did not show the GUI function, which triggers the stopping. Imagine this looks like this:
function StopButtonCallback(hObject, EventData, handles)
handles = guidata(hObject);
oldLength = numel(handles.Markers);
while numel(handles.Markers) == oldLength
pause(0.02); % Are you sure that the timer's stop callback is finished?!
handles = guidata(hObject);
Now what do you observe? Is the figure's handles struct updated?
I would not use the name "hObject" for the figure handle in the timer callback. "hFigure" would be easier to understand.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by