How to turn a movie into a gif?

13 visualizaciones (últimos 30 días)
Connor
Connor el 22 de Sept. de 2024
Comentada: DGM el 23 de Sept. de 2024
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)
Warning: Movie requested to play 0 times

Respuestas (2)

Walter Roberson
Walter Roberson el 22 de Sept. de 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 comentario
DGM
DGM el 23 de Sept. de 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

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 22 de Sept. de 2024

Categorías

Más información sobre Red en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by