Plotting continuous data from A/D converter

4 views (last 30 days)
I am using the session interface of Data Acq Toolbox to continuously take samples from a NIDAQ. I have created a listener, so that periodically the collected data is written to a file. At the same time, without stopping data collection, I want the ability to plot the data.
My code does this--but there is a gap in the plot between every call to the listener service routine. The data correctly plots to the same axes, but the subsequent plots are not connected. I have tried various permutations of hold, but none seem to do what I want. Here's the relevant code:
MATLAB code
% --- Processes incoming samples as they are available
function processData(src,data,fid,hObject)
% We have a block of samples
nsamp = size(data.Data,1);
numchans = size(data.Data,2);
handles=guidata(hObject);
for i=1:nsamp
fprintf(fid,'%6.4f ',data.TimeStamps(i));
for j=1:numchans
fprintf(fid,'%2.4f ',data.Data(i,j));
end
fprintf(fid,'\n');
end
if (handles.doPlot)
plot(handles.axes1,data.TimeStamps,data.Data(:,handles.sel_index));
% if first time, add the legend
if(handles.newplot)
legend(handles.axes1,handles.sel_legend);
handles.newplot=false;
guidata(hObject,handles);
end
end
Here's the result (notice the gap around 20 sec):

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Dec 2014
Jim - since you are plotting distinct blocks (of samples) then there will gaps between the curves (corresponding to the drawn block of data) on the axes. You would need to include the last point drawn for the previous block with the new block of data so that you get the continuous line being drawn from where the previous block ended and the new one begins. Or, you could just use one handle to the plot graphics object, and update its x and y data whenever your processData function gets called. Something like
function processData(src,data,fid,hObject)
% etc...
if (handles.doPlot)
% if first time, add the legend and create the plot
% graphics object
if(handles.newplot)
legend(handles.axes1,handles.sel_legend);
handles.newplot=false;
handles.hPlot = plot(NaN,NaN);
guidata(hObject,handles);
end
% update the x and y data
xdata = [get(handles.hPlot,'XData') data.TimeStamps];
ydata = [get(handles.hPlot,'YData') data.Data(:,handles.sel_index)];
set(handles.hPlot,'XData',xdata,'YData',ydata);
end
In the above, we use one plot graphics object to update the axes when new blocks of samples arrive..so it should ensure that there are no gaps between the data.
An alternative would be to keep track of the last point added to the axes, and insert that at the beginning of each of the timestamp and Data vectors prior to plotting it on the axes. This may be easier to implement - just update the handles structure with this point, and then insert it into your new data (always remembering to save the last point added).

More Answers (0)

Community Treasure Hunt

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

Start Hunting!