How can I animate a plot?

6 views (last 30 days)
Rares Mateizer
Rares Mateizer on 29 May 2015
Answered: Ingrid on 29 May 2015
Hello, I need some help.Since I am quite new to MATLAB, I will kindly ask you to not go hard on me, most of what I have learned I did by myself.So I have this plot and I want to make it move.After a couple of tries I think I ve got it, but it plots the same gif about 20 times.Also, it gives an error which states:
"Error using graphicsversion Input was not a valid graphics object
Error in getframe (line 51) usingMATLABClasses = ~graphicsversion(parentFig, 'handlegraphics');
Error in unda (line 26) getframe; "
What is wrong?Here is my code.Thanks for your help!
EDIT: I ve found out that only exiting the figure before the animation is complete yields the error.How to fix this?
clear all; close all; clc;
psi0=1;
lambda=1;
vphi=1;
k=2*pi/lambda;
omega=k*vphi; T=2*pi/omega;
[xx,yy]=meshgrid(linspace(-10,10,100),linspace(-10,10,100));
for t=0:0.005:2 %aici va fi ciclu in tema;
x=linspace(-3*lambda,3*lambda,100);
y=x;
for ix=1:length(x)
for iy=1:length(y)
ro=sqrt(x(ix)^2+y(iy)^2);
if(ro>lambda/20)
psi(iy,ix)=psi0/ro*sin(omega*t-k*ro);
end
end
end
surf(xx,yy,psi);
zlim([-10 10]);
drawnow;
getframe;
end
shading interp;
colormap spring;

Answers (1)

Ingrid
Ingrid on 29 May 2015
this code works fine when I run it. Looks pretty nice as well.
clear all; close all; clc;
psi0=1;
psi = zeros(100,100); % initializing variable for better memory management
lambda=1;
vphi=1;
k=2*pi/lambda;
omega=k*vphi; T=2*pi/omega;
[xx,yy]=meshgrid(linspace(-10,10,100),linspace(-10,10,100));
x=linspace(-3*lambda,3*lambda,100); % does not need to be within loop because not changing
y=x;
for t=0:0.005:2 %aici va fi ciclu in tema;
for ix=1:length(x)
for iy=1:length(y)
ro=sqrt(x(ix)^2+y(iy)^2);
if(ro>lambda/20)
psi(iy,ix)=psi0/ro*sin(omega*t-k*ro);
end
end
end
surf(xx,yy,psi);
zlim([-10 10]);
shading interp;
colormap spring;
drawnow;
end
but what are you trying to do? Do you want to save the movie to be played later outside of matlab? Than something like this might do the trick
clear all; close all; clc;
psi0=1;
psi = zeros(100,100); % initializing variable for better memory management
lambda=1;
vphi=1;
k=2*pi/lambda;
omega=k*vphi; T=2*pi/omega;
[xx,yy]=meshgrid(linspace(-10,10,100),linspace(-10,10,100));
x=linspace(-3*lambda,3*lambda,100); % does not need to be within loop because not changing
y=x;
idx = 1;
figure('DoubleBuffer','on','Renderer','painters');
for t=0:0.005:2 %aici va fi ciclu in tema;
for ix=1:length(x)
for iy=1:length(y)
ro=sqrt(x(ix)^2+y(iy)^2);
if(ro>lambda/20)
psi(iy,ix)=psi0/ro*sin(omega*t-k*ro);
end
end
end
surf(xx,yy,psi);
zlim([-10 10]);
shading interp;
colormap spring;
drawnow;
try
F(idx) = getframe(gcf);
catch
% if frame was closed before animation was finished than need to
% stop the code
return
end
idx = idx + 1;
end
movie(F)

Categories

Find more on Animation in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!