Continuous X-Axis plot in Real Time

5 Ansichten (letzte 30 Tage)
bilal malik
bilal malik am 12 Mär. 2017
Kommentiert: bilal malik am 18 Mär. 2017
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.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 12 Mär. 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 Kommentare
Geoff Hayes
Geoff Hayes am 18 Mär. 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
bilal malik
bilal malik am 18 Mär. 2017
That makes sense and has solved the problem. Thank you :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by