How to export (save) an animation video file (as mp4-format)

36 次查看(过去 30 天)
clear all;
close all;
clc;
count = 1;
%Declaring the balls initial conditions
R_Ball = 2;
initpos.x = 0;
initpos.y = 2.4;
initvel.x = 2;
initvel.y = 4;
gravity.x = 0;
gravity.y = 9.81;
restitution = 0.7;
GroundBall_friction = 0.2;
%Animation timestep
dt = 0.001;
%Executing the animation
pos.x = initpos.x; % initial position
pos.y = initpos.y; % initial position
vel.x = initvel.x; % initial velocity-x
vel.y = initvel.y; % initial velocity-y
t_arc = linspace(0,(2*vel.y)/gravity.y,2000);
for k = 1:2000
%Updating the ball's position
vel.x = vel.x;
vel.y = vel.y - gravity.y*t_arc(k);
pos.x = pos.x + vel.x*t_arc(k);
pos.y = pos.y + vel.y*t_arc(k) - (1/2)*gravity.y*(t_arc(k).^2);
if vel.y < 0 && pos.y < 0
vel.y = (-1)*(restitution)*vel.y;
vel.x = vel.x + GroundBall_friction*(restitution - 1)*vel.x;
end
pos.y = max(0, pos.y);
clf;
%Drawing the frame
subplot(2,1,1)
hold on
line([0 30],[0 0]);
rectangle('position', [pos.x pos.y R_Ball R_Ball],'Curvature',[1 1],'FaceColor','r');
posxx(count) = pos.x;
posyy(count) = pos.y;
plot(posxx + 1/2*R_Ball, posyy + 1/2*R_Ball,'b');
axis([0 30 0 10]);
hold off
subplot(2,1,2)
hold on
velyy(count) = vel.y;
velxx(count) = vel.x;
plot(posxx,velxx,'r','LineWidth',2);
plot(posxx,velyy,'b','LineWidth',2);
count = count+1;
legend('Velocity.X','Velocity.Y');
hold off
axis([0 30 -10 10]);
%Refresh rate
pause(dt)
end
This is my code, I want to export(save) it in an MP4 format so I can play the video for a presentation. I'm unaware how to do so, any help is appreciated.

采纳的回答

Image Analyst
Image Analyst 2017-2-12
编辑:Image Analyst 2021-7-17
See my attached demo. It converts a movie from .avi format to .mp4 format. Another attached demo makes a movie from an axes (showing a surface created by surf). The third demo shows how to create a movie from individual still frame images saved in files on disk.
  6 个评论
Jerome Blaha
Jerome Blaha 2023-6-3
Thank you for very concise and well written code examples, very nice.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by