Continuous X-Axis plot in Real Time
3 views (last 30 days)
Show older comments
I am plotting some data from arduino in Matlab in real-time. The problem is that I am unable to get the x-axis (samples) to increase in samples without starting from 0 again. So I have the data with an interval of 200 samples (interv = 200;) and the axis are plotted using: axis([0,interv,0,3]). All this is under a while(1) loop so it plots the graph continuously but once the samples reach 200, its starts to plot again from 0 and the process is repeated. So what I would like to do is once 200 is reached then continue the samples to display from 200-400 and then 400-600 and so on. I don't want to use auto-axis since it will just squash the data.
0 Comments
Accepted Answer
Geoff Hayes
on 12 Mar 2017
bilal - please see https://www.mathworks.com/matlabcentral/answers/284202-how-to-plot-a-real-time-signal-with-axes-automatically-updating which has an example that is similar to yours. It proposes that a timer be used to update the axes (rather than a while loop) and updates the data for the graphics object that was used to plot the data
xdata = [get(handles.hPlot,'XData') handles.T(end)];
ydata = [get(handles.hPlot,'YData') handles.array(end)];
set(handles.hPlot,'XData',xdata,'YData',data);
where
handles.hPlot = plot(NaN,NaN); % creates the graphics object with no data
Or, if you just want to set the limits on the x-axis, you could probably use xlim to set the limits on the x-axis given the current set of samples
numSamples = 200;
atBlock = 1;
xlim([(atBlock-1)*numSamples atBlock*numSamples]);
6 Comments
Geoff Hayes
on 18 Mar 2017
Hi bilal - you are closing the figure so the handle hPlot (of the graphics object drawn on the figure) is no longer valid. You're while loop doesn't know this and so throws an error when it tries to call
set(hPlot, 'YData', y);
since hPlot is no longer valid. I suppose you could put a check around this as
if ishandle(hPlot)
set(hPlot, 'YData', y);
else
break; % break out of the loop
end
You would then need another check in the outer while loop to decide whether you should break out of that one too
while(1)
k = 1;
while(t<currentInterval)
b=readVoltage(a, 'A0');
y=[y,b];
if ishandle(hPlot)
set(hPlot, 'YData', y);
else
break; % break out of the loop
end
xlabel('Samples')
ylabel('Voltage')
title('Phonocardiogram')
axis([currentInterval - intervalSize,currentInterval,0,3]);
grid
t=t+k;
pause(0.002)
end
currentInterval = currentInterval + intervalSize;
atInterval = atInterval + 1;
if ~ishandle(hPlot)
break;
end
end
% close connection to arduino
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!