Export Animated Line Plots as a Video in MATLAB

Creating an animated line plot and exporting it as a video could be done in the following steps:

1. Let's create three signals first with 60-degree phase differences

clear all;
clc;
x = linspace(0,4*pi,500);
y1 = sin(x);
y2 = sin(x-pi/3);
y3 = sin(x+pi/3);

2. Now let's create objects of animatedline() 

signal_1 = animatedline('Color', 'b', 'LineStyle', '-', 'LineWidth', 1) ;
signal_2 = animatedline('Color', 'r', 'LineStyle', '-', 'LineWidth', 1) ;
signal_3 = animatedline('Color', 'g', 'LineStyle', '-', 'LineWidth', 1) ;

3. Run the program in a loop and plot each points also grabbing frames in a cell

for i = 1 : length(x)
    addpoints(signal_1, x(i), y1(i));
    addpoints(signal_2, x(i), y2(i));
    addpoints(signal_3, x(i), y3(i));
    drawnow
    frames{i} = getframe(gcf) ;
end

4. Create a video writer object and write each frames

obj = VideoWriter('Signals.avi');
obj.Quality = 100;
obj.FrameRate = 40;
open(obj);
for i = 1:length(x)
    writeVideo(obj, frames{i}) ;
end
obj.close();

Watch the youtube video on Export Animated Line Plots as a Video in MATLAB




Download MATLAB code for exporting animated plot as a video

MATLAB Code

কোন মন্তব্য নেই

Blogger দ্বারা পরিচালিত.