TimerFcn within GUI function to another GUI function

1 view (last 30 days)
I am attempting to call one gui function from a gui function in a gui script so that I can pass various handles around.
Essentially how the program works is: Checkbox to stream data.
function Checkbox_Callback(hObjects,eventdata,handles)
onoff = get(hObject,'Value');
if onoff == 1
a = timer;
set(a,'executionMode','fixedRate','Period',1,'TimerFcn','Stream');
start(a)
elseif onoff == 0
stop(a)
end
The next function is used to call the data from an external program. The function definitely works, it's just the issue of how to call it correctly from the set(a,'TimerFcn',XXXXXXXXX);
The "Stream" function is along these lines:
function Stream(hObject,eventdata,handles)
XPNetConnect();
out = XPNetRecv();
variable1 = out.Variable1;
... etc
%Calculations
handles.Value = something;
guidata(hObject, handles);
Run_Another_Callback(hObject, eventdata, handles)
Hopefully you get the gist of what I'm trying to do.
I would really appreciate the help.
Thank you in advance.

Accepted Answer

Daniel Shub
Daniel Shub on 1 Mar 2012
I would do things a little differently ...
Make your timer object when you make your gui. If you do it at this point, all the handles that Stream needs are known.
You could do:
handles.htimer = timer('executionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @(h, evt)Stream(h, evt, handles));
Note the specification of TimerFcn as
@(h, evt)Stream(h, evt, handles)
You would then rewrite Checkbox_Callback to be
function Checkbox_Callback(hObject,eventdata,handles)
if get(hObject,'Value')
start(handles.htimer)
else
stop(handles.htimer)
end
  1 Comment
John
John on 2 Mar 2012
One thing though is... how do I stop the timer?
I get the following ever time I uncheck the box:
Reference to non-existent field 'htimer'.
Error in ROSv2>Stream_Data_Callback (line 975)
stop(handles.htimer)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in ROSv2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)ROSv2('Stream_Data_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
...and the timer continues to run.

Sign in to comment.

More Answers (2)

Sean de Wolski
Sean de Wolski on 1 Mar 2012
use setappdata/getappdata for the required handles and timerfind to find the appropriate timers.
  1 Comment
John
John on 1 Mar 2012
Thanks for the tips again. I'll try later and post back when working.
Cheers

Sign in to comment.


John
John on 2 Mar 2012
Hi,
Thank you for your input. What are the meanings of 'h' and 'evt'?
I rewrote what you put as the following:
function ROSv2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.htimer = timer('executionMode','fixedRate','Period',1,'TimerFcn',@(hObject,eventdata)Stream1(hObject,eventdata,handles));
then
function Stream_Data_Callback(hObject,eventdata,handles)
onoff = get(hObject,'Value');
if onoff == 1
start(handles.htimer)
else
stop(handles.htimer)
end
guidata(hObject, handles);
and
function Stream1(hObject, eventdata, handles)
...
...
...
...
But I get an error saying:
Error while evaluating TimerFcn for timer 'timer-1'
H must be the handle to a figure or figure descendent.
Not sure how to go about fixing this, I previously used the exact properties of the suggested code, i.e. I used the
@(h,evt)Stream1(h,evt,handles)
but got an error:
Error while evaluating TimerFcn for timer 'timer-1'
Undefined function or variable 'hObject'
as I call it in the function Stream1...
Thanks in advance again.
  3 Comments
Nope
Nope on 17 May 2012
What do you mean by that? I'm having the exact same problem and can't find a solution. What do you mean pass the additional variables, by the looks of the code you're passing all 3 variables already? Please help.
Cait
Cait on 13 Jul 2012
Yes, I am also experiencing the same problem. I get the "H must be the handle to a figure or figure descendent."
And then "Reference to non-existent field 'gotimer'", which is what I'm calling my timer the next time I call it.
What did you do?

Sign in to comment.

Categories

Find more on Programming Utilities in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!