MATLAB Answers


How do I animate this line plot?

Asked by Kelly McGuire on 30 Apr 2018
Latest activity Commented on by Ameer Hamza
on 1 May 2018
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

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.

1 Answer

Answer by Ameer Hamza
on 30 Apr 2018
 Accepted Answer

The following code will generate the moving animated line as you described in the question,
data = xlsread('WaterPerResidueMatrix.xls', 1);
animationWriter = VideoWriter('HSPtoHSPCircle');
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);
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.


Show 1 older comment
You just need to read documentation clearly. MATLAB has well-maintained documentation. Whenever you face a problem related to some function, just look at its documentation and you will find relevant information. Of course your ability to spot the relevant information increase with practice and experience.
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?
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.