How to update a graph with timer ?

11 views (last 30 days)
Alan Martínez
Alan Martínez on 20 Oct 2021
Answered: Geoff Hayes on 23 Nov 2021
I have a gui, where I want to update the values ​​of an analog sensor in PIC the data is already read updated but when generating the graph it marks an error which is the following:
Error while evaluating TimerFcn for timer 'timer-26'
Not enough input arguments.
My timer function is the following:
% --- Executes just before usbGuide is made visible.
function usbGuide_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to usbGuide (see VARARGIN)
% Choose default command line output for usbGuide
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
clc
global m_timer_daq
time = 0.1;
tasks = 100;
m_timer_daq = timer('StartDelay', 0, ...
'ExecutionMode', 'fixedRate');
m_timer_daq.Period = time;
m_timer_daq.TasksToExecute = tasks;
m_timer_daq.StartFcn = {@start_timer_callback, 'My start message'};
m_timer_daq.TimerFcn = @timerDAQ_callback_1;
%m_timer_daq.StartFcn = @(x,y)tic; global rawCurrent; rawCurrent=0;
m_timer_daq.StopFcn = @(x,y)toc;%delete(m_timer_daq);
%%plot(handles.axes1,salida_f);
% UIWAIT makes usbGuide wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function timerDAQ_callback_1(obj, event,handles)
obj.UserData = obj.UserData + 1;
global ctu
global salida_f
ctu=ctu+1
disp(salida_f)
plot(handles.axes1,salida_f);
function start_timer_callback(obj, event, text_arg,handles)
global ctu
ctu=0;
global salida_f
time = 0;
txt1 = ' event occurred at ';
txt2 = text_arg;
event_type = event.Type;
event_time = datestr(event.Data.time);
msg = [event_type txt1 event_time];
disp(msg)
disp(txt2)
tic
% --- Outputs from this function are returned to the command line.
function varargout = usbGuide_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;

Answers (1)

Geoff Hayes
Geoff Hayes on 23 Nov 2021
Hi @Alan Martínez - the default timer function callback signature takes two inputs only. You are using that default definition here
m_timer_daq.TimerFcn = @timerDAQ_callback_1;
but you have defined a timer callback function with three input parameters
function timerDAQ_callback_1(obj, event,handles)
and so the error message of there "not being enough input parameters" makes sense: the default definition is passing two, but your signature expects three. If you want to pass a custom (third) parameter into your callback, you need to do that when you assign the callback to your timer. Try the following
m_timer_daq.TimerFcn = {@timerDAQ_callback_1, hObject}
Since I am using hObject (the figure/GUI handle) instead of handles then you need to change your callback timer code to
function timerDAQ_callback_1(obj, event, hFigure)
handles = guidata(hFigure);
obj.UserData = obj.UserData + 1;
global ctu
global salida_f
ctu=ctu+1
disp(salida_f)
plot(handles.axes1,salida_f);
I do this because when you pass handles as a parameter to your timer callback, it is a copy of the handles structure only. So any changes to this object will not persist outside of the callback function (though you aren't making any changes anyway so I suppose it doesn't matter).
Also, consider removing the global parameters. I see that you are using them between the start and DAQ callback functions. Just save them to the handles structure instead.
Oh - I see that you will have the same problem with the start timer callback. You need to change that as well:
m_timer_daq.StartFcn = {@start_timer_callback, 'My start message', hObject};
and
function start_timer_callback(obj, event, text_arg, hFigure)
handles = guidata(hFigure);
handles.ctu = 0;
handles.salida_f = 0; % is this correct? salida_f is never assigned...
guidata(hFigure, handles); % save the updated handles structure
time = 0;
txt1 = ' event occurred at ';
txt2 = text_arg;
event_type = event.Type;
event_time = datestr(event.Data.time);
msg = [event_type txt1 event_time];
disp(msg)
disp(txt2)
tic
Then use handles in the timerDAQ_Callback to get the ctu and salida_f (which never appears to be set).

Categories

Find more on Specifying Target for Graphics Output 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!