-1

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: slide 1 slides 1![][1] slide3 slide4 slide5 slide 6 slide 7

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?

user2179716
  • 79
  • 1
  • 9
  • Here is a simillar question [How to create movies on each generation of a for loop in Matlab plot](http://stackoverflow.com/questions/16826780/how-to-create-movies-on-each-generation-of-a-for-loop-in-matlab-plot). Or just look at the last example in [`VideoWriter`](http://www.mathworks.com/help/matlab/ref/videowriterclass.html). Look also [`getframe`](http://www.mathworks.com/help/matlab/ref/getframe.html). Nice images by the way! – p8me Jun 01 '13 at 19:28
  • are you using MATLAB or python? Also, you should not re-post the same question multiple times. – tacaswell Jun 01 '13 at 19:41
  • @tcaswell. Am using Matlab. Sorry for the multiple posting. – user2179716 Jun 01 '13 at 20:11
  • @pm89, i have looked at both videowriter and getframe, but they not doing what i what? Maybe you edit the code above and implement on it let me see how its done, maybe i am not doing it right. – user2179716 Jun 01 '13 at 20:14
  • 1
    good catch @pm89 that question isn't just similar, it is the same question from the same user. – tacaswell Jun 01 '13 at 20:25
  • @tcaswell, yes i posted that question, but it was for how to plot contour of a sphere. I was able to do that. This is a different question. I hope it is not a problem. – user2179716 Jun 01 '13 at 20:38
  • both questions seem to boil down to 'how do I make a movie' – tacaswell Jun 01 '13 at 21:45
  • I suggested that @user2179716 ask a new question if he/she a specific question about actual code (with `VideoWriter` or another) that had been tried. The [previous question](http://stackoverflow.com/questions/16826780/) was pretty much answered as best possible. This question is nothing more than a repost with images that don't help us understand what the problems is (movies are mostly agnostic to their content). We can't help if you don't ask specific questions and/or provide code examples of what has been tried or doesn't work. – horchler Jun 01 '13 at 22:17
  • @horchler. Thanks for the clarification on the double posting issue. I sorry if i am not asking clearly what i want. Let me say i just want to plots contour of sphere superimpose with some generated values as shown on the slides posted above. Let me not say movie, but generation of the above steps in the pictures posted at each iteration. I hope it is clear now? The code tried is above and also the edited part is also tried but none working. – user2179716 Jun 01 '13 at 23:10

1 Answers1

0

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.

Community
  • 1
  • 1
p8me
  • 1,840
  • 1
  • 15
  • 23
  • i have tried your code but it have error which says "Undefined function or method 'VideoWriter' for input arguments of type 'char'" the error is at this code writerObj = VideoWriter('Your_video.avi'); – user2179716 Jun 02 '13 at 00:52
  • Then maybe your matlab version does not support this command, let me update the answer. – p8me Jun 02 '13 at 01:10
  • my matlab version is R2008b. – user2179716 Jun 02 '13 at 01:14
  • Oops, my mistake sorry. `fig_h` should be assigned to the output of `figure` not `plot` (Now it's correct). Or you could also set no input to the `getframe` command, like: `frame = getframe;`. – p8me Jun 02 '13 at 01:55
  • if you available on check, maybe it will be fast and easier to explain. Can we go into chat? – user2179716 Jun 02 '13 at 02:39
  • i did try your suggestion, but this only capture the whole thing, not the different stages. – user2179716 Jun 02 '13 at 13:50
  • I can't really come to chat. So I suppose you tried `avifile` and you got a movie (`*.avi`), right? If so, probably this movie is fast (15 frames per second), now you should adjust the `frame rate` or `fps`. This is again in documentation of [`avifile`](http://www.mathworks.com/help/matlab/ref/avifile.html), but I have updated the answer too. – p8me Jun 02 '13 at 17:07
  • @user2179716 Now check the edit, it might help. – p8me Jun 03 '13 at 12:39