How do I animate this line plot?

4 views (last 30 days)
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.
  1 Comment
Kelly McGuire
Kelly McGuire on 30 Apr 2018
Updated code. It is now plotting the first row of data in the matrix, but stops at the second row with the error: "Value must be a vector of numeric type". Error in line 34 l.XData(i,j) = xData(i,j);

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 30 Apr 2018
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.
  4 Comments
Kelly McGuire
Kelly McGuire on 30 Apr 2018
I will work on that. The plot looks great. Would it be simple to add a control line to the plot, one that doesn't move during the animation, it's just the initial # of Waters Per Residue line from the initial structure that would be static on plot as a comparison to the line that is moving?
Ameer Hamza
Ameer Hamza on 1 May 2018
You can add at the beginning,
l2 = line();
l2.XData = 1:4;
l2.YData = data(1,:);
hold on
Before the lines for l in the beginning.

Sign in to comment.

More Answers (0)

Categories

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!