Plotting continuous data from A/D converter

조회 수: 2 (최근 30일)
Jim
Jim 2014년 12월 30일
답변: Geoff Hayes 2014년 12월 30일
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):

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 12월 30일
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).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by