Search code examples
matlabvideomatlab-figure

Matlab code is generating video file but not writing any frames to the file


I have some code and I have used it to generate a graph which moves with time.

The issue I am having is that I want to make this graph into a movie file.

I have looked at the docs (https://www.mathworks.com/help/matlab/ref/videowriter.html) for this matter.

My code is as follows:

This section is just the setting up of variables (not where the problem lies).

clear; clc;
m =1.0; k =1.0; alpha = 0.1; b = 0.1;                                       %setting m, k, alpha and beta
M = diag([m m m m/2]);                                                      %defining Matrix M 
K =[[(2*k) (-k) 0 0];[-k (2*k) -k 0];[0 -k (2*k) -k];[0 0 -k k]];           %defining Matrix K
C = alpha.*M + b.*K;                                                        %define C 
s0 = 0.05;                                                                  %setting initial displacement
time = 0:0.1:50;                                                            %define time as a vector of 0-50 (given in question) with interval of 0.1 
timeStep =0.1;                                                              %define time step  
Keff =((1/(timeStep^2)).*M+(1/(2*timeStep).*C));
force =zeros(length(time),4);
displacement =zeros(length(time),4);
velocity =zeros(length(time),4);
acceleration =zeros(length(time),4); 
acceleration(1,:)=(force(1,:)-displacement(1,:)*(K))/M;                     %using the formula to find the initial acceleration at time =0
Xminus1=displacement(1,:)-velocity(1,:)*0.1+acceleration(1,:)*0.005;

for i=1:length(time)                                                        %use for loop to find the force at all times
    force(i,:)=[-0.05*sin(time(i)),0.1*sin(time(i)),0.1*cos(time(i)), 0];   %Use the formula given to find the force
end

for i=1:length(displacement)-1                                              %loop over all time step for each floor for the length of displacement
    if i==1                                                                 %since displacement(2) needs to be defined by Xminus1, we can filter out exceptions with an if statement
        displacement(2,:) =(force(i,:)-displacement(1,:)*(K-2/(timeStep^2)*M)-Xminus1*(1/(timeStep^2)*M-1/(2*timeStep)*C))/Keff;     %using the formula given for calculation of displacement(2)
    else                                                                    %else statement runs the rest of displacement(X)
        displacement(i+1,:) =(force(i,:)-displacement(i,:)*(K-2/(timeStep^2)*M)-displacement(i-1,:)*(1/(timeStep^2)*M-1/(2*timeStep)*C))/Keff;    %using the formula given for calculation of displacement
    end                                                                     %end if-else statement
end  

This is section has to do with the running of the code and generating the video:

vidObj = VideoWriter('test1.avi');
open(vidObj);
for i=1:length(time)

    figure (4)
    plot([0,0.5], [0 0],'r')                                                %floor base 
    hold on                                                                 %hold figure to fit multiple graphs
    axis([-0.3 0.8 0 2.2])                                                  %set axis scaling
    set(gca,'XTick',(-0.3:0.1:0.8))                                         %set graphics object properties
    set(gca,'YTick',(0:0.2:2.2))                                            %set graphics object properties

    %1st Floor
    plot([0 displacement(i,1)],[0,0.5], 'b')                                %left wall
    plot([0.5 displacement(i,1)+0.5], [0,0.5], 'b')                         %right wall
    plot([displacement(i,1),displacement(i,1)+0.5], [0.5 0.5],'r')
 hold off                                                                %turn hold off
    pause(0.01)                                                             %use pause of 0.1 seconds to reduce frame rate else there will be A LOT of windows
end 
close(vidObj)

Upon running this code, an avi file - test1.avi - is generated but it is 0 bytes and the command window says Warning: No Video Frames were written to this file. The file may be invalid.

I'm not sure what I'm doing wrong. Help is appreciated.


Solution

  • You miss the part of actually saving the current frame:

    frame = getframe(figure(4));
    writeVideo(vidObj,frame);
    

    Add these lines just after the pause in the loop creating the video.