animatedline plot with sliding window view
12 次查看(过去 30 天)
显示 更早的评论
So I have an hour long plot that I want to look at as an animation in 2 second increments.
r = [ 0 2];
for step = 2
for K = 1:length(t)
xlim(r)
addpoints(an,t(K),C14_theta(K));
drawnow limitrate
end
pause on
pause(5)
r = r + step;
xlim(r)
end
This is what I have currently. My issues that I need resolving are as follows:
- If I leave the r = r +step outside of my for loop then I can only see the first two seconds of my trace and the window doesn't pan.
- If i put both the pause and the r = r+ step in the for loop it pauses for 5 seconds every time it plots a data point as well.
- If I leave out the pause but put the r = r+ step in the main for loop the x axis changes so fast that I can't see any of my data points.
There are 40001 data points in a 2 second period, and it takes roughly 5 seconds to execute that. Please could somebody help so that my x scale is 0-2 for the first two seconds and during that time all of the data points in the first two seconds are plotted, and then I pan to x axis 2-4 and all of the points between 2 and 4 seconds are plotted.
0 个评论
回答(1 个)
Nitin Kapgate
2021-2-10
You can refer to the following code snippet to solve the issue you are facing.
The code demonstrates a way to display data for every seconds (with a 2 second pause in the updation of the graph).
For future reference, the plots can be exported to individual frames (containing 2 second data plot) and subsequently to a GIF file as demonstrated in the code below.
close all;
% Create data for 10 seconds, 5 datas every 2 seconds.
data = 1:5:125; % 25 data points
time = 0.2:0.2:10;
% Generate the frames to be written to a GIF
for k = 1:5
if(k==1) % Only create plot for the first iteration, update subsequently
figure();
hold('on'); grid('on');
h = plot(time(1:5), data(1:5));
pause(2); % Comment/Delete this line if no pause of 2 seconds is required
legend('Animated graph, Updated every 2 Seconds');
xlabel('Time (seconds)');
ylabel('Data');
else
set(h, 'xdata',time(k+1:k+5));
set(h, 'ydata',data(k+1:k+5));
pause(2); % Comment/Delete this line if no pause of 2 seconds is required
end
title(['k = ' num2str(k)]);
% Save the frame as png with a resolution of 150 pixels per inch
print(['Frame ' num2str(k)], '-dpng', '-r150');
end
% To generate the animated gif
GifName = 'AnimatedGraph.GIF';
delay = 2; % Delay between frames (2 seconds)
for j = 1:5
[A, ~] = imread(['Frame ' num2str(j) '.png']);
[X, map] = rgb2ind(A, 256);
if j == 1
imwrite(X, map, GifName, 'gif', 'LoopCount', inf, 'DelayTime', delay)
else
imwrite(X, map, GifName, 'gif', 'WriteMode', 'append', 'DelayTime', delay)
end
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!