I cannot access the handles in a timer callback function

I have a GUI with a timer in it. I would like to be able to update a plot every .1 seconds, however, the timer function gives me the error:
Error while evaluating TimerFcn for timer 'timer-3'
Reference to non-existent field 'concMarker'
Can anyone help me fix this? I have been reading some other posts and people have been saying you need to update the handles structure, which I am not quite sure how to do (though when I have tried copying other people's code to do so it did not work either).
axis = findobj(PumpRateGuiDisplay,'Type','axes');
timerData = timer('TimerFcn',@timerFunction,...
'Period',.1,'ExecutionMode','FixedRate','BusyMode','Drop',...
'TasksToExecute',endTime*10 + 1,'Tag','timerData');
function timerFunction(hObject,eventData)
handles = guihandles(PumpRateGuiDisplay);
handles.cocnMarker = plot(axis,NaN,NaN,'r*');
timeElapsed = get(hObject,'TasksExecuted')-1;
if timeElapsed ~= 0
set(handles.concMarker,'XData',times(int64(timeElapsed*.1/timeIncrement)),'YData',concLeftVent(int64(timeElapsed*.1/timeIncrement)),'*');
end
end
Thanks in advance for the help!

 Accepted Answer

handles.cocnMarker = plot(axis,NaN,NaN,'r*');
set(handles.concMarker,'XData',times(int64(timeElapsed*.1/timeIncrement)),'YData',concLeftVent(int64(timeElapsed*.1/timeIncrement)),'*');
Compare the name of the handle carefully between those two lines.

4 Comments

good catch, you're right, but this is not the problem (even after fixing this the same error still comes up, I only had the typo in the version of the code I put here). If it helps to diagnose the problem, I had the same problem when I tried to identify the axes by axes(handles.axis1) - matlab said axis1 did not exist when I know it did because something was already plotted on it, I had to get around that problem by using the findobj command though I cannot use that here because if I find all lines, I will find the line that is already plotted as well as the point I want to change.
You do not appear to have "hold on" in your axes, so each plot() that is fired up is going to cla
In another post I have just been tracking through and finding that in some cases where imshow() is used, that the current axes gets deleted the second time imshow() is called.
Ah, just did more probing: if the axes is the only axes in the figure and the axes is at the default position, then the first imshow() call sets up conditions so that the next time one of the high level graphics routines like imshow() or plot() is called, the axes will be deleted. (In particular the figure NextPlot property is set to 'replacechildren', and then the next time newplot() gets called, newplot() will delete all the children of the figure.)
If you happen to have that combination of circumstances, then you can get around the problem by issuing a "hold on" on the axes after the imshow()
This was obscure!
Thank you sooooooo much! I really appreciate the clear time and effort you spent on my question, I can honestly say you saved me many days of frustration and unproductiveness because I do not think I would have found this answer in a million years. I changed the next plot property for the axes to add and then put a hold on after my initial plotting of concMarker and now it works perfectly. Thanks again!
This was really obscure, and I had no idea that it happened.
I tend not to use imshow() because it changes too many properties for my liking, so I had never happened to encounter this before. It took digging.
I can see from the code that the behaviour is intended, but I filed a bug report anyhow to at least get it mentioned in the tips section.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!