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);
How do I animate this line plot?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kelly McGuire
el 30 de Abr. de 2018
Comentada: Ameer Hamza
el 1 de Mayo de 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.
Respuesta aceptada
Ameer Hamza
el 30 de Abr. de 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 comentarios
Ameer Hamza
el 1 de Mayo de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Animation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!