0

I'll do my best to explain my problem. I am simulating the dynamics of a network and I'd like to get an animation where each frame represents my network with a specific color for each node with respect to an input file. Here my script

 for ii=1:Movie_size

hfig=figure('visible','off');
hold on;

%plot edges
    for kk=1:Nedge,
        plot(xedge(kk,:),yedge(kk,:),'black')
    end

%color of the nodes
    for kk=1:nodes,
        val=(1-(Color_node(12798 ,kk)-umin)/(umax-umin));
        ggCol(kk,:)=[1,val,1-val];
    end

%enhanced the contrast of the figure
    ggCol = imadjust(ggCol,[.2 .3 0; .6 .7 1],[]);

%plot nodes
    for kk=1:nodes,
        plot(xpos(kk),ypos(kk),'o','MarkerFaceColor',ggCol(kk,:), ...
            'MarkerEdgeColor','k','MarkerSize',10)
    end

    frames(ii)=getframe(hfig);

    hold off;

end

movie(frames);

I succeeded in plotting each frame but when I want to get the animation, I have all the figures being displayed and no movie. I tried a lot of different things but it never works...

PS : I have been editing the title since the topic seems to have been already asked...

Hyppolite
  • 47
  • 5

1 Answers1

1

While you have already called getframe which takes a screen capture of the current figure, you need to do something with this frame to make a movie. The typical thing would be to add this frame to an existing VideoWriter object within your loop.

writer = VideoWriter('output.avi');

hfig = figure();
hplot = plot(rand(10,1));

for k = 1:100
    % Update the plot
    set(hplot, 'YData', rand(10, 1));

    % Take a screengrab and add it to the video file
    frame = getframe(hfig);
    writer.writeVideo(frame);
end

writer.close()

Alternately, you can create an array of frames and then display these interactively within MATLAB with movie.

for k = 1:100
    frames(k) = getframe(hfig);
end

% View as a movie
movie(frames)

Update

Based on your updated question, the windows have to popup because getframe must have the figure render before it is able to capture the screen. Also, you've created your array of frames but haven't attempted to display a movie. You need:

movie(frames)
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Sorry for answering with such delay, I was absent. I have been trying to do your way but I still get a lot errors. I think I do not manage really well with the loops, etc... Could you be a bit more specific in your example using pieces of my script ? Thanks and sorry again – Hyppolite Jan 08 '17 at 03:40
  • @Hyppolite Post the code you've tried and I can provide some feedback. – Suever Jan 08 '17 at 03:43
  • Since it has been pointed out as a duplicate I changed the title. With the script I now have I get the movie but my problem is that the figure are continuously popping during the execution of my script... – Hyppolite Jan 08 '17 at 13:47
  • @Hyppolite That is a completely different question...I have updated with a brief explanation of why that's happening. Also you've created your `frames` array but you never display it. – Suever Jan 08 '17 at 14:57
  • You're right I forgot to add it at the end but I was doing it on the terminal and it is working, I now just have to enhance a bit my movie and to find a way to export it properly. Also, just in order to know if that is possible, can I use a vector containing each time between two successive frame ? My data come from a stochastic simulation and the sampling time is not regular. Otherwise thanks for everything you have been really helpfull ! – Hyppolite Jan 08 '17 at 15:46
  • @Hyppolite To "export" it you want to use the `VideoWriter` piece that I have included above to export it frame by frame to a movie file. Also it is not possible to do variable frame rates – Suever Jan 08 '17 at 15:48