Creating Movie for each Generation of Data [duplicate]

喜夏-厌秋 提交于 2019-12-14 03:34:30

问题


I have the following code:

figure;
contour(X1,X2,f);

hold on

plot(top(1:size(top,1)), 'rx');

EDIT

    figure;
for i = 1: G
    contour(X1,X2,f);

    hold on

    plot(top(1:size(top,1)), 'rx');
end

NB: G is the maximum generation. This is supposed to plot contours of sphere superimposed with selected individuals. In each iteration of the individuals, the best individuals is selected and these going on until the global optimum is reached. I need to show this in a movie form as shown in this below:

When you runs each stage of the iteration is indicated in the slides attached. This is what i am trying to do. Any idea please?


回答1:


OK, I am just copying and pasting now, from here. However I added FrameRate (per second) since you might want to use (or ask) it later.

writerObj = VideoWriter('Your_video.avi');
writerObj .FrameRate = 1; % 1 frames per second animation.
open(writerObj);

fig_h = figure;
for i = 1: G
    contour(X1,X2,f);
    hold on
    plot(top(1:size(top,1)), 'rx');

    frame = getframe(fig_h); % or frame = getframe; since getframe gets gcf.
    writeVideo(writerObj, frame);
end

close(writerObj);

Now you will have a Your_video.avi file in your working directory.


If VideoWriter is not supported by your matlab, you could use use avifile same as mentioned in this answer (or in mathwork documentaion example here) like this:

aviobj = avifile('Your_video.avi','compression','None', 'fps', 1);

fig_h = figure;
for i = 1:G
    contour(X1,X2,f);
    hold on
    plot(top(1:size(top,1)), 'rx');

    frame = getframe(fig_h); % or frame = getframe; since getframe gets gcf.
    aviobj = addframe(aviobj, frame);
end

aviobj = close(aviobj);

EDIT

A problem may occur as pointed out by this question also, which is the captured frame is a constant image. If you are running Matlab on windows, this problem may be caused by conjunction of windows in with certain graphics drivers, and may be solved as mentioned in this answer.



来源:https://stackoverflow.com/questions/16875750/creating-movie-for-each-generation-of-data

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