Browsed by
Tag: Video

Creating AVI Videos in MATLAB

Creating AVI Videos in MATLAB

This video will demonstrate how to make a simple AVI video file of a MATLAB plot.

The following functions will be essential for this tutorial:

  • VideoWriter – This function create the video file. The input to the function is the video name.
  • getFrame – This function captures the current state of the figure which is to be written as a frame to the video file.
  • writeVideo This function write the capture frame to the video file.

I will also persistent variables. Persistent variables are local variables within a function that are retained for subsequent function calls. Nested functions will also be employed. Nested functions can be a bit confusing at first to a novice. However, they are an excellent way to keep code clean. Nested functions also have access to the variables within their parent function and thus it is not necessary to pass them through as inputs.

Source Code for Simple Video

function Made_Video_of_Equation

%% Setup the figure size and frame rate
xPixels = 1280;
FPS = 30; % frames per second
Ratio = 16/9;
yPixels = xPixels/Ratio;
AVI_Name = 'Simple_Video.avi';

T_Span = 5;

%% Create the figure
f = figure();
set(f, 'Units', 'pixels', 'Resize', 'off',...
    'MenuBar', 'none', 'ToolBar', 'none');

% Figure location on the screen, can be larger than screen resolution
f.Position = [50 50 xPixels yPixels];
hold on;
xlim([0 5]);
ylim([-1 1]);
grid('on');
xlabel('Time (s)');
ylabel('Amplitude');

%% Now Let's Plot
zeta = 0.1;
omega = 2*pi;
alpha = omega*zeta;
gamma = omega*sqrt(1-zeta^2);

t = linspace(0,5,T_Span*FPS);
y = exp(-alpha*t).*(alpha*sin(gamma*t)./gamma + cos(gamma*t));

for Frame = 1:length(t)
    
    % Update objects within figure and plot
    UpdateFigure(Frame)
    
    % Grab the frame and add to avi
    if Frame ~= length(t)
        CreateAvi(false)
    else
        CreateAvi(true)
    end
    pause(1/FPS);
end
    function UpdateFigure(Frame)
        % Persistent variables are stored between function calls and are
        % accessible in subsequent calls
        persistent h
        if Frame == 1
            % First frame, create object handle
            h = plot(t(1:Frame), y(1:Frame), 'LineWidth', 4);
        else
            set(h, 'XData', t(1:Frame), 'YData', y(1:Frame));
            set(gca, 'FontSize',18,...
                'FontName', 'Times New Roman');
            set(findall(gca,'type','text'),'FontSize',18,...
                'FontName', 'Times New Roman');
        end
        
    end

    function CreateAvi(FinalFrame)
        persistent CreateAVIFile
        frame = getframe(f);
        % Create the video
        
        if isempty(CreateAVIFile)
            % Create and open file during first call
            CreateAVIFile = VideoWriter(AVI_Name, 'Motion JPEG AVI');
            CreateAVIFile.Quality = 100;
            CreateAVIFile.FrameRate = FPS;
            open(CreateAVIFile);
            writeVideo(CreateAVIFile,frame)
        elseif FinalFrame
            % Close avi file on final frame
            writeVideo(CreateAVIFile,frame)
            close(CreateAVIFile)
        else
            writeVideo(CreateAVIFile,frame)
            % Already created, do nothing
        end
    end

end

The type of videos one can create with MATLAB can be rather complex. The video below is a portion which was used to plot and track orbital debris in Earth orbit (debris not included in this snippet). The source code is included below the video file. Credit should be given to Ryan Gray for the snippet of code used to map the texture map to the globe. Keep in mind, due to the large size of the image used for the texture map, this video may take a while to render. Render time can be decreased by reducing the texture map image size.

Source Code for Globe Video
Globe_Files