animatedline plot with sliding window view

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.

Answers (1)

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

1 Comment

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);
figure
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');
open(v);
for K =point1: 5 :point2
addpoints(an,t(K),C14_theta(K));
xlim([limits(K),(limits(K)+2)])
drawnow limitrate nocallbacks
frame = getframe(gcf);
writeVideo(v, frame);
end
close(v)
hold off
But thank you very much indeed for your suggestion!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 5 Feb 2021

Commented:

on 12 Feb 2021

Community Treasure Hunt

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

Start Hunting!