MATLAB: How to animate this line plot

animatedlineplot

I am trying to make an animated line plot changing based on the number of water molecules at a given amino acid inside an ion channel. I have some initial code to do this. The plot is created the way I want for the most part, but I'm not sure what to do with the for loop part. Because the x-data is just the residue (# of waters vs Residue), so the x-data doesn't change, but a line needs a y-coordinate and an x-coordinate. An example of the plot and the matrix is attached, which is a plot from just the first row of # of water molecules in the 1667 x 4 matrix, as well as the modified code so far. At each of those 4 residues, the # number of waters is changing each frame, but I didn't want to plot just moving points, but an animated line of the # of waters changing. A line like the plot in the .xls attached file.

Best Answer

The following code will generate the moving animated line as you described in the question,
data = xlsread('WaterPerResidueMatrix.xls', 1);
animationWriter = VideoWriter('HSPtoHSPCircle');
open(animationWriter);
stepsBetweenTwoLines = 10;
l = line();
l.XData = 1:4;
l.YData = [];
ax = gca;
ax.XLim = [1 4];
ax.XTick = 1:4;
ax.XTickLabel = {'residue 1', 'residue 2', 'residue 3', 'residue 4'};
t = linspace(0, 1, stepsBetweenTwoLines);
for i = 1:size(data, 1)-1
% create data for circle plot or any other
% processing you want to do
line1 = data(i, :)';
line2 = data(i+1, :)';
line = line1*(1-t) + line2*t;
for j = line
l.YData = j;
frame = getframe(gcf);
writeVideo(animationWriter, frame);
end
end
close(animationWriter);
Here is the output obtained from this on my PC.
As you can see I forgot to add YLim, therefore, the y-axis keep moving. Also, you can control speed by changing animation writer.FrameRate or the stepsBetweenTwoLines variable i defined above. Similarly, you can add other formatting options e.g. line thickness, color, etc.