毫无疑问,这个问题已被提出;但是,经过一段时间的搜索后,我找不到任何与我的情况有关的内容。
我要做的是制作一部通过for循环生成的情节电影,然后导出这部电影文件。我怎么能这样做?这是我的代码的主要部分:
for j=1:N
a=randi(L);
b=randi(L);
c=randi(L);
d=randi(L);
e=randi(L);
f=randi(L);
% Calculate energy at positions
if lattice(a,b,c)==1 && lattice(d,e,f)==0
%latticenew=lattice;
%latticenew(d,e,f)=1;
%latticenew(a,b,c)=0;
E2=EnergyMC_1(lattice,mu,eps);
dE = E2-E1; %Change in total energy of the system (gas)
%as the particle goes from its current positon to the randomly
%selected position.
%DeltaE = [DeltaE,dE];
%Nsteps=Nsteps+1; %What does Nsteps do?
if rand<exp(-dE*beta) %Factor of K is in there to reduce
%the inverse temperature beta, so that the for loop can
%be longer than 15 iterations.
lattice(d,e,f)=1;
lattice(a,b,c)=0;
%lattice=latticenew;
E1=E2;
Accepted=Accepted+1;
%drawnow
[x,y,z] = ind2sub(size(lattice),find(lattice==1));
%position = [x,y,z];
%timelattice(:,:,j)=position;
plot3(x(:),y(:),z(:),'r.')
grid
%This is where the plotting occurs
end
HistoryE=[HistoryE, E1];
end
waitbar(j/N,H);
end
编辑:
好的,我打开了提供的链接,并尝试按照链接中的说明进行操作。但是,我无法让它发挥作用。它制作一个电影情节,然后终止,给我错误
Error using avi
Unexpected Error. Reason: Failed to find the open file handle.
Error in avifile/addframe (line 188)
avi('addframe',rot90(frame,-1), aviobj.Bitmapheader, ...
Error in Lattice_Project4 (line 104)
aviobj = addframe(aviobj,F);
我真的不明白所说的是什么。这是我最新更新的代码:
for j=1:N
a=randi(L);
b=randi(L);
c=randi(L);
d=randi(L);
e=randi(L);
f=randi(L);
% Calculate energy at positions
if lattice(a,b,c)==1 && lattice(d,e,f)==0
%latticenew=lattice;
%latticenew(d,e,f)=1;
%latticenew(a,b,c)=0;
E2=EnergyMC_1(lattice,mu,eps);
dE = E2-E1; %Change in total energy of the system (gas)
%as the particle goes from its current positon to the randomly
%selected position.
%DeltaE = [DeltaE,dE];
%Nsteps=Nsteps+1; %What does Nsteps do?
if rand<exp(-dE*beta) %Factor of K is in there to reduce
%the inverse temperature beta, so that the for loop can
%be longer than 15 iterations.
lattice(d,e,f)=1;
lattice(a,b,c)=0;
%lattice=latticenew;
E1=E2;
Accepted=Accepted+1;
[x,y,z] = ind2sub(size(lattice),find(lattice==1));
%position = [x,y,z];
%timelattice(:,:,j)=position;
%drawnow
plot3(x(:),y(:),z(:),'r.')
%camorbit(1+j/10000,0);
grid
F=getframe(fig);
aviobj = addframe(aviobj,F);
end
HistoryE=[HistoryE, E1];
close(fig);
aviobj = close(aviobj);
end
%waitbar(j/N,H);
end
答案 0 :(得分:1)
在“这是绘图出现的地方”之后,写下:
Mymovie(j)=getframe;
然后在代码结束后,您可以按照这些答案How to make and save a video(avi) in matlab
保存电影