MATLAB getframe captures whatever is on screen

paul simmons picture paul simmons · Dec 19, 2011 · Viewed 15.4k times · Source

I am trying to create a movie from my MATLAB plot. When I call getframe, it "usually" captures the plot image, but sometimes if there is something else active on the screen (which is normal if I continue to use the computer) it captures whatever window is active. Is there another way to grab the image of the active figure?

e.g.

fig = figure;
aviobj = avifile('sample.avi','compression','None');
for i=1:t
    clf(fig);
    plot(...); % some arbitrary plotting
    hold on;
    plot(...); % some other arbitrary plotting
    axis([0 50 0 50]);
    aviobj = addframe(aviobj, getframe(fig));
end
aviobj = close(aviobj);

Answer

paul simmons picture paul simmons · Dec 19, 2011

OK, found the solution; instead of

aviobj = addframe(aviobj, getframe(fig));

sending the figure handle directly to addframe is enough:

aviobj = addframe(aviobj, fig);