How to turn a movie into a gif?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I am trying to turn this script into a .gif file. I tried using movie2gif, imwrite, and write but was unsuccesfull. Below is the script. Any help would be greatly appreciated.
% Bounds and resolution of mesh
x=0:0.1:10;
t=0:300:10000;
[X,T]=meshgrid(x,t);
% Constants
i=sqrt(-1);
a=10; % Angstroms
m=9.1*(10^-31); % Kg
h_bar=1.05*(10^-34); % J*s
% Energy Levels
E_1=((pi*h_bar)^2)/(2*m*a^2);
E_2=((4*pi*h_bar)^2)/(2*m*a^2);
E_3=((9*pi*h_bar)^2)/(2*m*a^2);
% Wavefunction
psi=sqrt(1/3).*(exp(-i*T*E_1/h_bar).*sin(pi*X/a)+exp(-i*T*E_2/h_bar).*sin(2*pi*X/a)+exp(-i*T*E_3/h_bar).*sin(3*pi*X/a));
% Modulus of wavefunction
mod=conj(psi).*psi;
% Functions to be plotted
u_1=real(mod);
u_2=real(psi);
u_3=imag(psi);
% Capturing frames
F(length(t))=struct('cdata',[],'colormap',[]);
for i=1:length(t)
plot(x,u_1(i,:),'linewidth',1.5);
hold on
plot(x,u_2(i,:),'linewidth',1.5);
plot(x,u_3(i,:),'linewidth',1.5);
hold off
xlabel('Postion Inside infinite square well (Å)');
legend('|\psi(x,t)|^2', 'Re(\psi)', 'Im(\psi)');
axis([min(x) max(x) min(min(u_3)) max(max(u_1))]);
F(i)=getframe;
end
close
movie(F,0)
0 commentaires
Réponses (2)
Walter Roberson
le 22 Sep 2024
Before the for i loop insert
filename = 'Output.gif';
Change
F(i)=getframe;
to
F(i)=getframe;
if i == 1;
SZ = size(F(1).cdata);
imwrite(F(1).cdata, filename, 'writemode', 'overwrite');
else
F(i).cdata = imresize(F(i).cdata, SZ);
imwrite(F(i).cdata, filename, 'writemode', 'append');
end
1 commentaire
DGM
le 23 Sep 2024
Need to convert the frames to indexed color. Probably should also set frame delay and loop count.
By default, getframe() grabs the current axes. If you want the axis labels, title, or ticks, you'll need to specify the figure instead.
% Bounds and resolution of mesh
x=0:0.1:10;
t=0:300:10000;
[X,T]=meshgrid(x,t);
% Constants
i=sqrt(-1);
a=10; % Angstroms
m=9.1*(10^-31); % Kg
h_bar=1.05*(10^-34); % J*s
% Energy Levels
E_1=((pi*h_bar)^2)/(2*m*a^2);
E_2=((4*pi*h_bar)^2)/(2*m*a^2);
E_3=((9*pi*h_bar)^2)/(2*m*a^2);
% Wavefunction
psi=sqrt(1/3).*(exp(-i*T*E_1/h_bar).*sin(pi*X/a)+exp(-i*T*E_2/h_bar).*sin(2*pi*X/a)+exp(-i*T*E_3/h_bar).*sin(3*pi*X/a));
% Modulus of wavefunction
mod=conj(psi).*psi;
% Functions to be plotted
u_1=real(mod);
u_2=real(psi);
u_3=imag(psi);
% gif parameters
filename = 'test.gif';
fdelay = 0.05;
loopcount = Inf;
% Capturing frames
for i=1:length(t)
plot(x,u_1(i,:),'linewidth',1.5);
hold on
plot(x,u_2(i,:),'linewidth',1.5);
plot(x,u_3(i,:),'linewidth',1.5);
hold off
xlabel('Postion Inside infinite square well (Å)');
legend('|\psi(x,t)|^2', 'Re(\psi)', 'Im(\psi)');
axis([min(x) max(x) min(min(u_3)) max(max(u_1))]);
% i'm assuming that we don't need to keep the frames
% also assuming that frame size variation can be better solved
% either through managing the figure setup,
% or by doing post-capture padding on the entire frame stack.
thisframe = frame2im(getframe(gcf));
[thisframe CT] = rgb2ind(thisframe,256);
if i == 1
imwrite(thisframe, CT, filename, 'writemode', 'overwrite', ...
'delaytime',fdelay,'loopcount',loopcount);
else
imwrite(thisframe, CT, filename, 'writemode', 'append', ...
'delaytime',fdelay);
end
end
close
Image Analyst
le 22 Sep 2024
See the official Matrhworks support team Answer: https://www.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab?s_tid=srchtitle
0 commentaires
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!