Getframe very slow in loop

조회 수: 26 (최근 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