Updating a graph by changing which column of a matrix is being plotted to create an animation.
8 views (last 30 days)
Show older comments
I am trying to simulate the deformation of a rotor blade under turbulence, I would like to create an animation to show the bending at each time step.
I have an x vector that is constant and a square matrix for the amplitude that has the deflection at each x coordinate (rows) at each time step (columns). What I would like to do is to update which column is being plotted on my graph as the script runs to keep the graph open and show a an animation of the deformation at each timestep.
As an example if I had the following data
L=0.3
X=linspace(0,L,4);
Amplitude=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
Here is what I am currently trying:
for k=1:length(X)-1
g(k)=plot(X,Amplitude(:,k),'LineWidth',8);
g(k+1)=plot(X,Amplitude(:,k+1),'LineWidth',8);
delete(g(k))
end
But when I do this I just have 1 line from the last column being plotted. I have tried looking through the documentation and using drawnow and animatedline but so far I haven't really been able to get it to work. I'm still a novice when it comes to matlab and I suspect there is a command or a quick fix for this but I can't seem to figure it out. Any help would be much apprecieated!
0 Comments
Accepted Answer
Cris LaPierre
on 21 Apr 2023
I would probably do this by creating the first plot outside the for loop, then use the for loop to update the XData and YData properties of the line.
L=0.3
X=linspace(0,L,4);
Amplitude=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
g=plot(X,Amplitude(:,1),'LineWidth',8);
for k=2:length(X)
g.YData = Amplitude(:,k);
pause(1)
end
More Answers (2)
Image Analyst
on 21 Apr 2023
You may be interested in this attached demo that creates a sequence of plots and writes them to a movie file.
I have lots of other movie demos. If you want them, just ask.
Mathieu NOE
on 21 Apr 2023
hello
try this
L = 0.3000;
X=linspace(0,L,4);
Amplitude=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
figure(1)
p = plot(X,Amplitude(:,1),'LineWidth',8); %// initiallize plot. Get a handle to graphic object
axis([0 L 1 20]); %// freeze axes to their final size, to prevent Matlab from rescaling them dynamically
for k=2:length(X)
% update the plot
pause(0.5)
set(p, 'XData', X, 'YData', Amplitude(:,k));
end
0 Comments
See Also
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!