问题
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:
- Initialized your
F
matrix. - 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