MATLAB: Findobj fails in timer call back

timer

I was testing a timer in a large program written in GUIDE 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 findobj command in the timer function callback, IsDataReady, returns a valid handle to figure1 the first time it is called. After that it only returns null.

Best Answer

I provide the solution to this problem because it took days to find; mostly because it was buried deep in the guts of our medium size deployable application.
In GUIDE, the main figure, figure1, had it’s HandleVisibility set to callback which is the default. This means that “handles are visible from within callback routines or function invoked by callback routines, but not from within functions invoked from the command line.” HandleVisibility can also set to ‘on’ or ‘off’. In the above code, the first time IsDataReady was called it was as a callback and thus findobj found the handle. Every time after that the timer must be executing in a different space and findobj returns null. The solution is to change HandleVisibility of the parent figure to ‘on’ or to use the findall command.
fHandles = findall(0,type,figure,tag,figure1)
Here the zero signifies the root object and thus all the children are searched.