Getframe very slow in loop

24 views (last 30 days)
Manav Babel
Manav Babel on 13 Oct 2020
Answered: Gaurav Garg on 16 Oct 2020
I'm simulating a wave, and animating it by plotting each time step in a loop and using getframe() to store each frame in a struct. I'm using a movie instead of using pause to animate because I'd later like to save the movie.
When I comment out the getframe call, the entire script takes less than half a second to run, but with it, it takes more than 30 seconds.
u is the 2D array used to store the wave height, where each column is a position and each row is a timestep.
How do I make getframe take less time, or, failing that, is there another way I can animate the wave?
% Initialises the figure, makes it invisible so the movie is the only
% thing seen.
figure('visible','off');
hold on
% Sets y limits, with padding
ylim([min(u,[],'all') - 0.1 * range(u,'all'), max(u,[],'all') + 0.1*range(u,'all')]);
% Initialises the object used to store the image frames
F(length(t)) = struct('cdata',[],'colormap',[]);
% Plots the initial position of the wave and stores the frame
p = plot(x, u(1, :));
F(1) = getframe(gcf);
% Animates the wave
for i = 2:length(t)
% Updates the wave and stores the frame
set(p, 'YData', u(i, :));
F(i) = getframe(gcf);
end
% Makes figure visible again
figure('visible', 'on');
% Displays the movie
movie(gcf, F, 1, fps);

Answers (1)

Gaurav Garg
Gaurav Garg on 16 Oct 2020
Hey Manav,
getFrame captures the whole frame of the current figure and saves it into a struct. So, it is expected to take this amount of time.

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!