Getframe very slow in loop

23 次查看(过去 30 天)
Manav Babel
Manav Babel 2020-10-13
回答: Gaurav Garg 2020-10-16
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);

回答(1 个)

Gaurav Garg
Gaurav Garg 2020-10-16
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.

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by