Clear Filters
Clear Filters

make gif of visual simulation

8 views (last 30 days)
Thomas Kirven
Thomas Kirven on 27 Feb 2017
Commented: Walter Roberson on 27 Feb 2017
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

Answers (1)

Walter Roberson
Walter Roberson on 27 Feb 2017
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 Comments
Thomas Kirven
Thomas Kirven on 27 Feb 2017
Edited: Thomas Kirven on 27 Feb 2017
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 on 27 Feb 2017
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.

Sign in to comment.

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!