Findobj not working on timer callback
I was testing the timer in a large GUID program and I came across these strange results.
function checkfordone_openningFcn(hObject, eventdata, handles, varargin)
…
handles.t = timer(‘TimerFcn’,@IsDataReady,’ExecutionMode’,’fixedRate’,’Period,4);
start(handles.t)
function IsDataReady(timerObject, eventdata)
fHandles = findobj(‘type’,’figure’,’tag’,’figure1’)
end
The callback function of the timer function, IsDataReady, returns a valid handle to Figure1 the first time it is called. After that, it only returns null.
source to share
I provide a solution to this problem because it took days to search; mainly because it was buried deep in the guts of our mid-sized deployable application.
In the GUIDE, the main figure, figure1, has set its HandleVisibility to callback, which is the default. This means that "the handles are visible from inside callbacks or a function called by callbacks, but not from functions called from the command line." HandleVisibility can also be set to "on" or "off". In the above code, the first time IsDataReady was called, it was like a callback, and thus findobj found the handle. Each time thereafter, the timer has to run in a different space and findobj returns null. The solution is to change the HandleVisibility of the parent shape to "on" or to use the findall command.
fHandles = findall (0, 'type, figure, tag, figure1)
Here zero means the root object and hence all children are searched.
source to share