在matlab中保存的电影无法播放

时间:2017-03-25 12:55:31

标签: matlab matlab-figure matlab-guide

我在matlab中创建了一部电影,我想保存它。我写了下面的代码来做到这一点,它在代码运行正常时显示电影,但是已经保存的文件无法播放。错误是:"无法确定流的类型"

clear all
close all
l = 27;
r = 4; noise_t = 0.02; v0 = 0.1; nn = 300;
x = rand(1,nn).*l; y = rand(1,nn).*l; vx = 0.1.*(rand(1,nn)-0.5.*ones(1,nn)); vy = 0.1.*(rand(1,nn)-0.5.*ones(1,nn)); 
figure('Color',[0 0 0])
axis([0 115 0 40])
axis('square')
hold on
vidObj = VideoWriter('che.avi');
open(vidObj);
time = 900;
neigh=[];
eigh=[];
for t = 1:time;
eig=[];
for number = 1:nn;
distance(1:nn) = (ones(1,nn).*x(number)-x).^2 + (ones(1,nn).*y(number)-y).^2;
neighbour_s = distance < r^2;
eig(1,nn) = sum(neighbour_s);
a = dot(double(neighbour_s),vx)/(sum(neighbour_s));
b = dot(double(neighbour_s),vy)/(sum(neighbour_s));
vx1(number) = a/norm([b,a]); vy1(number) = b/norm([b,a]); mvx(number) = vx1(number); mvy(number) = vy1(number);
end
vx_center = noise_t .*(rand(1,nn)-0.5); vy_center = noise_t .*(rand(1,nn)-0.5); 
vx = mvx + vx_center; vy = mvy + vy_center;    
x = x + vx; y = y + vy;
index = (x < 0); x(index) = rem(x(index),l) + l;
index = (y < 0); y(index) = rem(y(index),l) + l;
index = (x > l); x(index) = rem(x(index),l);
index = (y > l); y(index) = rem(y(index),l);
eigh = [eigh;eig]; 
cla
vecc=eigh(:);
vecc= vecc(find(vecc>0));
hist(vecc,80)
drawnow
currFrame = getframe;
writeVideo(vidObj,currFrame);
drawnow
end

1 个答案:

答案 0 :(得分:2)

当您向其中写入新帧时,有必要使用close method显式关闭VideoWriter对象。这将完成视频并使其可播放。

% At the end of your script
close(vidObj)