Nathan Chan
Nathan Chan

Reputation: 177

Animate through multiple 2D Matlab plots

I have multiple 2D line plots in Matlab (they represent some wave moving through space). Each plot represents the wave at some time t. I want to animate through these plots (i.e. show the first plot for a fraction of a second, then show the next one, and the next, etc. I want to loop back to the beginning once it reaches the end time) to show the time evolution of the system. surf and mesh don't really do what I want since it is too difficult to see the changes with the number of time steps I have. Is there a way to do this in Matlab?

Upvotes: 1

Views: 495

Answers (1)

Matt
Matt

Reputation: 13943

I assume with "2d-line" you mean a 2d-plot. This is done by the plot-function, so there is no need of surf or mesh. Sorry, when I got you wrong.

The following code does what I think you asked for:

% Generate some propagating wave
n = 20;
t = linspace(0,10,100);
x = cell(1,n);
for i = 1:n
    x{i} = (1-abs(i/n-0.4))*sin(t+i*0.2);
end

% Create frames
figure;
for i = 1:length(x)
   clf;
   plot(t,x{i});
   ylim([-1,1]);
   myFrames(i) = getframe;  %#ok<SAGROW>
end

% Show movie
figure;
movie(myFrames,2,2);    % frames, repetitions, frames per second

Upvotes: 2

Related Questions