在Matlab中制作电影并将其导出为一些电影文件



毫无疑问,这个问题已经被问过了;然而,找了相当长一段时间后,我没有找到任何与我的情况有关的东西。

我想做的是制作一个我通过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

在"This is the plot happens "之后,写:

Mymovie(j)=getframe;

然后在代码结束后,您可以根据这些答案保存电影如何在matlab中制作和保存视频(avi)

最新更新