How to create movies on each generation of a for loop in Matlab plot

…衆ロ難τιáo~ 提交于 2019-12-04 06:48:27

问题


I want to plot the sphere function as a surface or a contour plot, and the position and fitness value of the best individual which are evaluated by a sphere function of data generated from a Gaussian distribution superimposed with it. The plot will change in each generation so i get a movie. Also, on another figure, i want to plot the contour of the sphere function superimposed with the entire population generated from Gaussian distribution, with the retained fit individuals colored in red. This from generation to generation should give another movie.

This is basically an implementation of estimation of distribution algorithm(EDA). Anyone know how to do this?

EDIT

K= 4 
for l = 1 : K 
contour(X,Y,ph); 
hold on 
plot(bestId, 'rx'); 
end 

The above code should superimpose contour plot and BestId. For each loop of l, a bestId is generated and superimposed with the contour plot. This generation of BestInd should take place for each l. but among the generations of bestId from 1 to 4, there is one which is the best of them and we should get that after four generation which is superimposed with the contour plot. Now i want this generation to be a movie for reach iteration from 1 to 4, so that i can see how the bestId are generated untill the best(optimal) one is achieved. this is what i meant by movie in matlab. Any idea on how this could be done?


回答1:


To create movie reflecting changes in figures, I am using combination of the class avifile and functions getframe() and addframe()

Here is an example

aviobj = avifile('example.avi','compression','None');

t = linspace(0,2.5*pi,40);
fact = 10*sin(t);
fig=figure;
[x,y,z] = peaks;
for k=1:length(fact)
    h = surf(x,y,fact(k)*z);
    axis([-3 3 -3 3 -80 80])
    axis off
    caxis([-90 90])

    F = getframe(fig);
    aviobj = addframe(aviobj,F);
end
close(fig);
aviobj = close(aviobj);

You can find more info here

http://www.mathworks.nl/help/matlab/ref/avifile.html

http://www.mathworks.nl/help/matlab/ref/movie.html

http://www.math.canterbury.ac.nz/~c.scarrott/MATLAB_Movies/movies.html

-----------------Edit after the discussion in the comments------------------

pm89 suggested another way in the comments. The VideoWriter class seems more modern and up to date. The example of use can be found at the end of the page below

http://www.mathworks.nl/help/matlab/ref/videowriterclass.html



来源:https://stackoverflow.com/questions/16826780/how-to-create-movies-on-each-generation-of-a-for-loop-in-matlab-plot

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