animatedline plot with sliding window view

24 views (last 30 days)
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)
drawnow limitrate
pause on
r = r + step;
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.

Answers (1)

Nitin Kapgate
Nitin Kapgate on 10 Feb 2021
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
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)');
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
title(['k = ' num2str(k)]);
% Save the frame as png with a resolution of 150 pixels per inch
print(['Frame ' num2str(k)], '-dpng', '-r150');
% 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)
imwrite(X, map, GifName, 'gif', 'WriteMode', 'append', 'DelayTime', delay)
  1 Comment
Anna van der Stap
Anna van der Stap on 12 Feb 2021
Thank you very much! I actually already solved it for myself in the meantime by just creating an extra matrix with the corresponding limits of the axis so that I could index into it in the same way. As follows:
Time1 = ;
Time2 = ;
point1 = find(t == Time1);
point2 = find(t == Time2);
an = animatedline('MaximumNumPoints',length(t));
two_seconds = 40001;
lims = 0:2:max(t);
limits = repelem(lims,(two_seconds-1));
ylim([-0.2000, 0.2000]
v = VideoWriter('seizure.avi');
for K =point1: 5 :point2
drawnow limitrate nocallbacks
frame = getframe(gcf);
writeVideo(v, frame);
hold off
But thank you very much indeed for your suggestion!

Sign in to comment.


Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by