How to create a movie?

조회 수: 47 (최근 30일)
Rajawarman Thiruselvam
Rajawarman Thiruselvam 2021년 7월 8일
답변: Bhavya Chopra 2021년 7월 8일
Hi everyone!! i have a code for animated line, here how to create a movie for this program??
clear all
close all
clc
figure
h1=animatedline('linewidth',3,'color','[0 0 0.5]');
h2=animatedline('linewidth',3,'color','[1 0.5 0]');
axis([0 71 1 20])
axis ij
grid on
x1 =0:71;
n=100;
xx1=linspace(x1(1),x1(end),n);
y1=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
yy1=interp1(x1,y1,xx1);
y2=[2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3];
yy2=interp1(x1,y2,xx1);
for ci=1:n
addpoints(h1,xx1(ci),yy1(ci));
addpoints(h2,xx1(ci),yy2(ci));
pause(0.05);
drawnow
end

채택된 답변

Bhavya Chopra
Bhavya Chopra 2021년 7월 8일
I understand that you want to save your animated plot as a video. The program can be modified as follows to save the plot:
outputVideo = VideoWriter(fullfile(pwd, 'plot_movie.avi')); % Create VideoWriter object
outputVideo.FrameRate = 20; % Set frame rate for corresponding pause duration
open(outputVideo)
for ci=1:n
addpoints(h1,xx1(ci),yy1(ci));
addpoints(h2,xx1(ci),yy2(ci));
pause(0.05);
drawnow
img = getframe % Obtain figure as movie frame
writeVideo(outputVideo, img); % Write image to video
end
close(outputVideo) % Close video file
Please refer to documentation for getframe function and VideoWriter for more information.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by