考虑这个例子:
%# lets create a 10-by-10 matrix, of values in the range [0,9]
M = fspecial('gaussian',10,2.5);
M = (M-min(M(:))) ./ range(M(:));
M = round(M*9);
%# prepare video output
vid = VideoWriter('vid.avi');
vidObj.Quality = 100;
vid.FrameRate = 5;
open(vid);
%# display matrix
h = imagesc(M);
axis square
caxis([0 10])
colormap(jet(10))
colorbar
%# capture frame
writeVideo(vid,getframe);
%# iterate changing matrix
for i=1:50
M = rem(M+1,10); %# circular increment
set(h, 'CData',M) %# update displayed matrix
writeVideo(vid,getframe); %# capture frame
drawnow %# force redisplay
end
%# close and save video output
close(vid);
您可以使用自定义颜色表,只需创建一个大小为10×3的矩阵cmap
,每行包含RGB值并将其传递给呼叫colormap(cmap)
对于老版本的MATLAB比R2010b中,你可以使用avifile功能,而不是VideoWriter:
%# prepare video output
vid = avifile('vid.avi', 'fps',5, 'quality',100);
%# iterations
for i=1:50
%# ...
%# capture frame
vid = addframe(vid, getframe(gcf));
drawnow
end
%# close and save video output
vid = close(vid);
谢谢,我只是不明白你为什么这样做:M =(M-分钟(M(:)) )./范围(M(:));当范围(M(:))是1并且M-min(M(:))除以1时是相同的数字? – kojikurac
当我写作时,怎么会这样:vid = VideoWriter('vid.avi'); matlab返回:??? 'char'类型的输入参数未定义函数或方法'VideoWriter'? – kojikurac
@kojikurac:前三行只是为了创建一个样例矩阵,就像您所描述的那样(包含从0到9的自然数)。这应该用您拥有的实际数据替换。另一方面,VideoWriter是在MATLAB R2010b中引入的。如果你有一个旧版本,你可以使用旧的'avifile'功能。我更新答案以显示如何使用此功能。 – Amro