Matlab代码正在生成视频文件,但未向该文件写入任何帧

时间:2018-12-04 07:33:35

标签: matlab video matlab-figure

我有一些代码,并用它来生成随时间变化的图形。

我遇到的问题是我想将此图形制作成电影文件。

我已经查看了文档(https://www.mathworks.com/help/matlab/ref/videowriter.html)。

我的代码如下:

这部分只是变量的设置(不是问题所在)。

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  

这部分与代码的运行和生成视频有关:

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)

运行此代码时,将生成一个avi文件-test1.avi-但其为0字节,并且命令窗口显示警告:该文件未写入任何视频帧。该文件可能无效。

我不确定自己在做什么错。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您错过了实际保存当前帧的部分:

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

在创建视频的循环中暂停后添加这些行。