"H must be the handle to a figure or figure descendent" error using timers with a GUI
Show older comments
So I'm trying to make a threshold-crossing interval algorithm to detect ventricular fibrillation and wanted to make a few things (contained in the function "my_callback_function") happen every 1 second. I set up a timer with the line
handles.t=timer('TimerFcn', {@TCIfunc,handles.ecg},'ExecutionMode','FixedRate','Period',1,'StartDelay',1);
The "TCIfunc" function goes as follows:
function TCIfunc(hObject,eventdata,handles)
handles=guidata(handles);
handles.t1==handles.t3;
handles.t3==toc;
tic;
handles.t2_flag==0;
%start(handles.t);
handles.threshold==0.2*handles.max;
handles.max==0;
handles.N == 0;
guidata(handles.ecg, handles);
I declared t1 etc. using handles before creating the timer, in the opening function which includes the timer activation, seen here:
function ecg_OpeningFcn(hObject, eventdata, handles, varargin)
delete (instrfind)
handles.s = serial('COM11');
set(handles.s, 'InputBufferSize', 50); %number of bytes in input buffer
set(handles.s, 'FlowControl', 'none');
set(handles.s, 'BaudRate', 19200);
set(handles.s, 'Parity', 'none');
set(handles.s, 'DataBits',8);
set(handles.s, 'StopBits', 1);
set(handles.s, 'Timeout',1);
handles.threshold_flag=0;
handles.t2_flag=0;
handles.max=0;
handles.threshold=0;
handles.TCI_counter=0;
handles.N=0;
handles.t1=0;
handles.t2=0;
handles.t3=0;
handles.t4=0;
handles.ecg=gcf;
handles.t=timer('TimerFcn', {@TCIfunc,handles.ecg},'ExecutionMode','FixedRate','Period',1,'StartDelay',1);
guidata(handles.ecg,handles);
start(handles.t);
guidata(hObject, handles);
tic;
handles.output = hObject;
guidata(hObject, handles);
varargout{1} = handles.output;
But I keep getting the error: "Error while evaluating TimerFcn for timer 'timer-25'
H must be the handle to a figure or figure descendent." Anyone know what's going on? I'd be very grateful for any help. Full disclosure: I'm relatively new to MATLAB and don't have much idea what I'm doing.
3 Comments
Jan
on 17 May 2012
While the code after the 2nd line does not matter for the problem, the part, where "handles.t1" is defined (or should be defined) is important. Please post this also by adding it to the original question, not as comment or answer.
Nope
on 17 May 2012
Daniel Shub
on 18 May 2012
The error refers to H, but I (and Firefox) cannot find H anywhere in the code.
Answers (1)
Walter Roberson
on 17 May 2012
Please rename your "handles" parameter for your timer function to reflect that you are passing in a figure number rather than a handles structure. You will appreciate the change the next time you go to debug your code after not having looked at it for a day.
Then at the command window, put in
dbstop if error
and run again, and see what you get.
The statement,
handles.ecg=gcf;
Try using
handles.ecg = hObject;
7 Comments
Nope
on 17 May 2012
Walter Roberson
on 17 May 2012
Why are you using "==" in your timer function?
Have you tried changing the timer function to something like
@(src, evt) disp('Timer invoked')
just to see if you get that far?
Nope
on 17 May 2012
Nope
on 17 May 2012
Walter Roberson
on 17 May 2012
You have
handles.t3=toc;
tic;
The first time that runs, you are trying to find the elapsed time between a non-existent previous tic() and now. You cannot use toc() without a previous tic(). I am not sure what you are trying to do, but perhaps you should tic() in the routine that creates the timer.
Nope
on 17 May 2012
Walter Roberson
on 17 May 2012
Hmmm, current code please.
Categories
Find more on Creating and Concatenating Matrices 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!