How do I export an animated figure to some video format for use in a presentation?
Show older comments
With code adapted from other help threads, I have an animated figure which plays a video (about 20 sec) in one subplot while simultaneously plotting associated data in the next subplot. I run the code in Live editor (and just normally, for that matter) and the figure works perfectly.
How do I get this animated figure into a video file of some kind for use in a presentation? (code below)
Incidentally, the live editor includes a small export button at the bottom right of the figure, but this seems to freeze the code and I only get a blank gray section when I click it. (pics)
%setup subplots
ax1 = subplot(2,1,1); % For video
ax2 = subplot(2,1,2); % For data plot
%setup videoreader object
CH47Landing = 'D:\Manual Backup\PROJECTS\Ice\Engineer Lake\Landing.mp4';
v = VideoReader(CH47Landing);
nFrames = v.Duration*v.FrameRate; % Number of frames
% Display the first frame in the top subplot
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
%Load Data
lt = TIMESTAMP; %use your actual data
y = ld5;
nDataPoints = length(lt); % Number of data points
step = 0.033; % Increase this value to get a higher plot rate
index = 1:step:nDataPoints;
i = 2;
% Diplay the plot corresponds to the first frame in the bottom subplot
h = plot(ax2,lt(1:floor(index(i))),y(1:floor(index(i))),'-k');
%Fix the Axes
ax2.XLim = [lt(1) lt(end)];
ax2.YLim = [min(y) max(y)];
%Animate
while hasFrame(v)
pause(1/v.FrameRate);
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
i = i + 1;
set(h,'YData',y(1:floor(index(i))), 'XData', lt(1:floor(index(i))))
subplot(2,1,1)
xlim([1 1921])
ylim([1 925])
subplot(2,1,2)
set(gca, 'YDir','reverse')
ylabel('Deflection (mm)')
xticklabels({'12:02:00 PPM','12:02:15 PM','12:02:29PM'})
title('CH47; 31Jan23; h(i)=70cm; AirT=18°F. PT=#5')
xlim([datetime(2023,2,16,12,2,0)...
datetime(2023,2,16,12,2,40)])
ylim([0 15])
end

Accepted Answer
More Answers (2)
Anton Kogios
on 22 Feb 2023
Edited: Anton Kogios
on 22 Feb 2023
Documentation for recording animations: https://mathworks.com/help/matlab/creating_plots/record-animation-for-playback.html
and exporting animations: https://mathworks.com/help/symbolic/writeanimation.html
In your while loop, at the end add
vid(i) = getframe(gcf);
and then at the end of the script add
v = VideoWriter('recordedAnimation,'MPEG-4');
open(v)
for i = 1:length(vid)
writeVideo(v,vid(i))
end
close(v)
You may have to modify the above code since I cannot test it given you haven't provided the Landing.mp4 video. There is plenty of helpful documention if you search this topic up on Google. This may also help: https://mathworks.com/matlabcentral/answers/455886-how-to-save-animated-plots
7 Comments
Daniel
on 28 Feb 2023
Anton Kogios
on 28 Feb 2023
Sorry I should have mentioned that you need to initialise vid:
vid(1:n) = struct('cdata',zeros(420,560,3,'uint8'),'colormap',[]);
n is the number of frames, and 420 and 560 is the size/resolution of your figure window.
Daniel
on 28 Feb 2023
Daniel
on 1 Mar 2023
Anton Kogios
on 1 Mar 2023
Edited: Anton Kogios
on 1 Mar 2023
Instead of a while loop, read the number of frames in the video and set up a for loop. Something like:
for j = 1:nFrames
...
end
Daniel
on 1 Mar 2023
Anton Kogios
on 1 Mar 2023
Edited: Anton Kogios
on 1 Mar 2023
This should be at the very end, outside the for loop (the while loop in your code above):
v = VideoWriter('recordedAnimation','MPEG-4');
open(v)
for i = 1:length(vid)
writeVideo(v,vid(i))
end
close(v)
And this should be before the for loop (the while loop in your code above):
vid(1:990) = struct('cdata',zeros(420,560,3,'uint8'),'colormap',[]);
It may be best if you start with a simple example first and get that to work. You should also read the documentation I mentioned in my first answer so you know what the code is actually doing, rather than copy-pasting what I say at the incorrect locations.
Simon Chan
on 22 Feb 2023
0 votes
If you accept an animated GIF file as the output, check this link:
Categories
Find more on Audio and Video Data 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!