timer error in guide
13 views (last 30 days)
Show older comments
hello everybody.
I'm trying to implement a timer object om guide. the idiea is to invoke a function every couple of second.
the function monitor check ip data and renders the relevant cell in the table repetedly
I saw an example but somehow it doesnt work well I get the following error:
Error while evaluating TimerFcn for timer 'timer-10'
H must be the handle to a figure or figure descendent.
my code: function app_OpeningFcn(hObject, eventdata, handles, varargin) .
.
handles.timer = timer(... 'ExecutionMode', 'fixedSpacing', ... % Run timer repeatedly 'Period', 1, ... % Initial period is 1 sec. 'TimerFcn', {@uitable2_CellEditCallback,handles});
*the function:*
function uitable2_CellEditCallback(hObject, eventdata, handles)
guidata(hObject)
str='wmic path Win32_PerfRawData_Tcpip_NetworkInterface Get BytesReceivedPersec';
[~,cmdout] = system(str);
cmdout=cmdout(24:end);len=(length(cmdout)-2)/23;
recv=cell(len,1);
for ii=0:len-1
recv(ii+1)={cmdout(ii*23+1:ii*23+23)};
end
rate=cellfun(@(x) str2num(x),recv,'UniformOutput' , false);rate=[rate{:}]';
handles.current=rate(~rate(:)==0);lngth=length(handles.current);
handles.uitable2.Data(1:lngth,4)=num2cell(rate(~rate(:)==0));
guidata(hObject,handles)
I get the error message when I call this function:
function pushbutton6_Callback(hObject, eventdata, handles)
if strcmp(get(handles.timer, 'Running'), 'off')
start(handles.timer);
end
I would appriciate any help,
Elad
0 Comments
Answers (2)
Steven Lord
on 3 Aug 2016
Quoting the relevant lines with formatting applied, your call to timer is:
handles.timer = timer(... 'ExecutionMode', 'fixedSpacing', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@uitable2_CellEditCallback,handles});
By the way you specify your TimerFcn, uitable2_CellEditCallback will be called with the third argument a copy of the handles structure AS IT EXISTS AT THIS MOMENT. Any changes you make to the handles structure later on WILL NOT BE REFLECTED in the copy passed into your callback. [The emphasis on those two phrases is because I've seen a lot of people confused by this in the past, and it can be difficult to realize the cause of the problem.]
If this is your intention, you can use this approach; otherwise I would pass the handle to something fixed, where the handle will not change, into the TimerFcn and retrieve the handles structure using that handle. I would probably use the handle of the main figure in your GUI, as it's unlikely you're going to close that main figure without first stopping the timer.
Now on to the main issue. Your TimerFcn is, in part:
function uitable2_CellEditCallback(hObject, eventdata, handles)
guidata(hObject)
"When you create a callback function, the first two arguments must be a handle
to the timer object and an event structure."
The guidata function expects you to pass a graphics object handle as its input, not a timer object. If you use my suggestion from above and pass the handle to your main GUI figure in as the third input, that GUI figure handle would make a fine input to guidata.
0 Comments
Elad
on 3 Aug 2016
1 Comment
Steven Lord
on 3 Aug 2016
When the timer object calls your TimerFcn, it will automatically pass the timer object and the event data into your TimerFcn. You must define your TimerFcn to accept at least two inputs plus any additional inputs that you want to pass in. See the examples on the documentation page to which I linked.
See Also
Categories
Find more on Code Execution 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!