"H must be the handle to a figure or figure descendent" error using timers with a GUI

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

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.
Thanks, sorry about that. As you can see I've made a little bit of progress, but now I'm getting a different error message.
The error refers to H, but I (and Firefox) cannot find H anywhere in the code.

Sign in to comment.

Answers (1)

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

Heh, thanks for the tip on the "handles" thing.
As for the other two things, using "dbstop if error" doesn't stop it at any stage. I open the GUI and it runs fine (it's supposed to plot an input from a circuit in addition to the threshold crossing thing), but it clearly isn't activating the timer or calling the function (I put a display message in the TCIfunc function just to be sure and it didn't pop up at any stage). At the end, it gave the same error message about handles to figures or figure descendants. Using handles.ecg=hObject makes no difference to this. I'm not really sure what's going on; any ideas?
Thanks again.
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?
Oops. The == was from an old, incorrect version. Ignore them and pretend that they're = signs.
And yeah, putting disp('Timer invoked') into the timer function itself (instead of TCIfunc), displays it every 1 second, as expected. But if I put disp('Time invoked') in the actual TCIfunc function and call the function in the timer, it does nothing. I'm pretty sure this has more to do with passing the handles to the function than it does with the timer itself (although I still have no idea what that figure descendents error message means).
I should also note that I just noticed I'm also getting a different error message now. This one says "??? Error while evaluating TimerFcn for timer 'timer-1'
You must call TIC without an output argument before calling TOC without an input argument."
Any idea what that means? I really can't think of what I changed to change the error message, this is very weird.
Oh, and now I'm back to the old figure descendents error again.
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.
I do tic in the routine that creates the timer, dunno what that message was about. As I said, it's back to the figure descendents one.

Sign in to comment.

Categories

Asked:

on 17 May 2012

Community Treasure Hunt

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

Start Hunting!