Deleting plot point by point

4 次查看(过去 30 天)
elchico
elchico 2018-6-19
Hello,
I want to simulate a heart beating graph (like on an ECG = electrocardiogram). For that I have the data for this plot and I want to animate the line, so it is appearing point by point and after the last point is plotted, it starts again (https://www.youtube.com/watch?v=XV11kplLoxw).
I have the code for the plot appearing in the graph (so "first" time it runs through), but then I do not know how to "delete" the line point by point from the beginning, so I can plot the graph again point by point.
My first try was to paint over the plotted line with a white one (and the same data), but it does not work perfectly ...
I hope the question is understandable, else please ask the missing points.
Thanks in advance.
Regards,
elchico
.
My code without the "deleting step":
filename = 'heartbeating_data.dat';% incl. extension
delimiter = '\t';
M_RTC = dlmread(filename,delimiter);
time = M_RTC(:,1);
data = M_RTC(:,2);
a1 = animatedline('Color','r', 'LineWidth',2);
title('Heartbeating')
xlabel('time / s')
ylabel('|Z| (norm., 4 kHz)')
axis([0 10 0.99 1.03])
x = time;
for k = 1:length(x);
% first line
xk = x(k,1);
y = data(k,1);
addpoints(a1,xk,y);
% update screen
drawnow %limitrate
pause(0.0005);
end
My code with the painting over:
filename = 'heartbeating_data.dat';
delimiter = '\t';
M_RTC = dlmread(filename,delimiter);
time = M_RTC(:,1);
data = M_RTC(:,2);
a1 = animatedline('Color','r', 'LineWidth',2);
a2 = animatedline('Color','w', 'LineWidth',10);
title('Heartbeating')
xlabel('time / s')
ylabel('|Z| (norm., 4 kHz)')
axis([0 10 0.99 1.03])
x = time;
for ii = 1:10
for k = 1:length(x);
% red line
xr = x(k,1);
yr = data(k,1);
addpoints(a1,xr,yr);
% white line
if k < (length(x)-9)
xw = x(k+9,1);
yw = data(k+9,1);
elseif k < length(x)
xw = x(length(x)-k,1);
yw = data(length(x)-k,1);
else
xw = 12;
yw = 12;
end
addpoints(a2,xw,yw);
% update screen
drawnow %limitrate
pause(0.0005);
end
end

回答(1 个)

Antoine Bridet
Antoine Bridet 2018-6-19
Instead of reading and plotting the points one by one using addpoints, you might want to consider storing the data in an array and replot the entire graph at each run of the loop. At the beginning, the array would fill up and it wouldn't change the behaviour of your program, and once you start replacing the values at the beginning of the array, replotting would mean that you get the result that you are after.
Short version: no need to 'delete' points if you replace them in an array.
I hope this will help you!

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by