Matlab plot3电影,每个点的颜色根据任意向量而变化

时间:2015-05-21 16:54:34

标签: matlab plot colors 3d movie

我有一组nx6的数据数组,其中前3列是轨迹的x,y,z坐标,n是变化的。我在matlab中编写了一个脚本,允许我连续绘制每个点,然后将其制作成电影,这里是代码:

% attemt to make a movie! 
test = cou{7}(:,1:3) % data arrays are stored in larger cell arrays
numPoints = length(test(:,1))
x = test(:,1)
y = test(:,2)
z = test(:,3)
h = figure(1)
set(h,'Position',[100 678 560 420]) 

for j=1:numPoints
   plot3(test(1:j,1), test(1:j,2),test(1:j,3),'kd-')
   grid('on')
   xlim([min(x), max(x)])
   ylim([min(y), max(y)])
   zlim([min(z), max(z)])

   M(j) = getframe(h);
end

movie(M,1,30)
movie2avi(M,'testMovie2.avi')

这很有效,我以.avi格式制作了一部电影。我得到钻石每个< x,y,z>坐标是和连接到下一个点的线。

但是,我需要根据nx6矩阵中的第5列添加不同的颜色。在第5列中有0-8的数字,每个数字都需要特定的颜色,因此每个新数据点的颜色都是该列中唯一的颜色(0表示红色,1表示蓝色等。 )。

从我在网上阅读的所有内容看来,我必须关闭一种颜色或将这些点分成:

plot3(x1,y1,z1,s1,x2,y2,z2,s2, ...)

其中x1,y1,z1,s1将指定第一个坐标,其中s定义颜色,标记和线,x2,y2,z2,s2指定第二个点及其规格等。

最好,我可以为颜色规范定义某种矩阵S,以便我可以将它添加到图中:

  plot3(x(1:j),y(1:j),z(1:j),S(1:j))

到目前为止,我见过的所有相关帖子一直在询问/回答不同的3D情节类型。轨迹中的点遍布全局。所以我真的需要这种情节类型才能制作电影!

干杯, 丽莎

P.S。我意识到它不是最有效的代码,现在我写它的方式我可以摆脱测试数组。我大部分时间都在研究它,它已经演变成了这个。我只是想让它工作然后我会编辑它的效率!

1 个答案:

答案 0 :(得分:0)

可能的解决方案可能是:

  • 定义颜色indeces矩阵:因为你需要9种颜色它应该是一个(9 x 3)矩阵
  • 使用存储在数据第5列中的值来选择颜色矩阵中的颜色(即选择颜色矩阵的行)
  • 通过在color函数调用中指定参数“plot3”,将所选颜色设置为连接两个点的每一行
  • 您还可以通过设置markerfacecolor参数
  • ,使用所选颜色填充点标记

为此,您必须修改循环中“test”数据的索引;实际上,就像现在一样,每次迭代都会绘制1 to j中的所有点,而你应该从j to j+1绘制它们。

由于此修改,您还应添加hold on

您可以在下面的代码中找到建议的更新。

注意:

我必须对代码的第一部分进行注释,并添加一些指令来生成测试数据。

更重要

我有一个警告,执行代码的最后一行,关于压缩因子和编解码器。

Warning: Cannot locate Indeo5 compressor, using 'None' as the
compression type.

当我运行原始代码和运行带有更新的代码时,我都收到了这个警告,因此它应该不依赖于我所做的修改。

% attemt to make a movie! 
% test = cou{7}(:,1:3) % data arrays are stored in larger cell arrays
% numPoints = length(test(:,1))
% x = test(:,1)
% y = test(:,2)
% z = test(:,3)
% 
% Definition of example X, Y, Z points
% 
t=0:.3:2*pi;
x=cos(t);
y=sin(t);
z=(x.*y);
test=[x' y' z'];
numPoints = length(test)
% 
% Definition of color indeces
% 
test(:,5)=randi(9,numPoints,1);

h = figure(1)
set(h,'Position',[100 678 560 420]) 
% 
% Definiton of the set of colors
% 
line_col=[...
   0 0 1
   0 1 0
   0 1 1
   1 0 0
   1 0 1
   1 .1 .3
   .5 .5 .5
   .3 1 .3
   .7 .3 .9]

for j=1:numPoints-1
%
% Original plot instruction
% 
% % % plot3(test(1:j,1), test(1:j,2),test(1:j,3),'kd-')
% 
%    Updated plot instruction:
%       remove "k" color specification
%       specified 'color' and 'markerfacecolor' parameters
%       modified the indexig of the "test" dataset
% 
  a=plot3(test(j:j+1,1), test(j:j+1,2),test(j:j+1,3),'d-','color', ...
  line_col(test(j,5)+1,:),'markerfacecolor',line_col(test(j,5)+1,:), ...
  'linewidth',2)
   grid('on')
   xlim([min(x), max(x)])
   ylim([min(y), max(y)])
   zlim([min(z), max(z)])
% 
%    Addded "hold on" instruction, needed following the modification of the
%    indexing (see comment above)
% 
   hold on
   M(j) = getframe(h);
end

movie(M,1,30)
movie2avi(M,'testMovie2.avi')

enter image description here

希望这有帮助。