Exporting movie from matlab

一个人想着一个人 提交于 2020-01-16 20:29:31

问题


I am trying to make a movie in matlab.

for i=1:runs;
       for k=1:NO_TIMES
       B = 0;
       [conf dom_size] = conf_update_massmedia(time(k),conf);


       shortconf=conf(1:N);
       for j=1:N;
       sigma(j,1:N) = conf(1+(j-1)*N:j*N);
       end
    figure(1)
    imagesc(sigma);
    colorbar;
    set(gcf,'PaperPositionMode','auto');
    F(k)=getframe(gcf);
    end

end

movie2avi(F,'B=0.avi','Compression','none')

So my problem is that i only get a movie from the last run of the loop, i have tried to move the code for the figure around, but nothing seems to work, is there anyone who are able to help?

Pall


回答1:


movie2avi is a little outdated and struggles on various operating systems. A better option is using the VideoWriter command:

vidObj = VideoWriter('B=0.avi');
vidObj.FrameRate=23;
open(vidObj);

for i=1:runs;
   for k=1:NO_TIMES
      B = 0;
      [conf dom_size] = conf_update_massmedia(time(k),conf);
      shortconf=conf(1:N);

      for j=1:N;
         sigma(j,1:N) = conf(1+(j-1)*N:j*N);
      end

      figure(1)
         imagesc(sigma);
         colorbar;
         set(gcf,'PaperPositionMode','auto');

      F=getframe(gcf);
      writeVideo(vidObj,F);
    end
end

close(vidObj);



回答2:


As @tmpearce mentioned, the problem is because of overwriting the F matrix.

I suggest you to:

  1. Initialized your F matrix.
  2. Always indent you code to make it readable (see here for example).

This is one of the million solutions:

f_ind = 1; % Frame index.
F = zeros(runs * NO_TIMES, 1); % initialization of Frames matrix.
figure; % remove figure(1) from your inner loop haowever.
for i = 1:runs;
    for k = 1:NO_TIMES
        % ...
        F(f_ind)=getframe(gcf);
        f_ind = f_ind + 1;
    end
end


来源:https://stackoverflow.com/questions/16875492/exporting-movie-from-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!