make gif of visual simulation

8 次查看(过去 30 天)
Thomas Kirven
Thomas Kirven 2017-2-27
Is there a way to record a series of plotted frames and save it as a gif?
I have a simulation of a quadrotor that I can plot in real time: I simply plot two crossed lines in the position and orientation of the quadrotor and then delete this and update it as the simulation updates. As a result the visual simulation demonstrates the dynamics in real time and appears realistic.
However if I try to add any sort of frame capture the simulation becomes painfully tedious to watch. Is there a way to simply record each frame and play it back as a gif at whatever speed I want? I tried to do this using movie(fig,F,150) where F is an array containing the frames, but this plays very slowly - nowhere near 150 hz which is the simulation update rate.
-Thanks
Thomas

回答(1 个)

Walter Roberson
Walter Roberson 2017-2-27
Yes, there is an explanation in https://www.mathworks.com/help/matlab/ref/imwrite.html#btv452g-1 imwrite() "Write Animated Gif"
"I simply plot two crossed lines in the position and orientation of the quadrotor and then delete this and update it as the simulation updates"
Instead of doing that, draw the plot once and record the handle of what you need to update. Then in each iteration of the loop, update the XData and YData properties (and whatever label.) This is a lot faster than deleting the object and replotting.
  2 个评论
Thomas Kirven
Thomas Kirven 2017-2-27
编辑:Thomas Kirven 2017-2-27
Thanks for the reply Walter,
When you say draw the plot do you mean the command 'draw now' and record the figure handle? I tried this and it is much much slower.
Also I should say it's a 3-d plot.
Walter Roberson
Walter Roberson 2017-2-27
ax = gca();
max_frame = 1000;
frames(max_frame) = struct('cdata', [], 'colormap', []);
for counter = 1 : maxframe
if counter == 1
h = plot3(ax, ....)
title(ax, ...)
xlabel(ax, ....)
set(ax, 'xlimmode', 'manual', 'ylimmode', 'manual');
else
set(h, 'XData', new_x, 'YData', new_y, 'ZData', new_z);
end
drawnow()
frames(counter) = getframe(ax);
end
Then afterwards you can write the data in frames into an animated gif.

请先登录,再进行评论。

类别

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