MATLAB Answers

How to create a movie?

1 view (last 30 days)
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

Accepted Answer

Bhavya Chopra
Bhavya Chopra on 8 Jul 2021
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.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by