MATLAB: Plot 3D curve with color-coded direction

plot3 curve color-coded

hi, friends,
I have a 3xN matrix, which represents N points in one curve. I know plot3 can plot this curve easily, but I want to use color to represent the curve orientation. For example, if the curve is in x direction, the color is "red", if the curve is in y direction, the color is in "green", if the curve is in z direction, the color is in "blue“.
So the color will vary with the curve going from the beginning to the end.
I saw some example from other software, want to know whether it can be done in matlab.
Appreciate!
LZ

Best Answer

Yes, you can do this in MATLAB. But it is easier to use a "surface" (in this case, I am calling MESH) to plot it instead of a line.
%Make a random 3d curve
P = interpft(rand(10,3),500);
P(end+1,:) = P(1,:);
% Make colors from the velocity
dP = diff(P([1:end 1],:));
C = abs(dP)/max(abs(dP(:)));
C = permute(cat(3,C,C),[1 3 2]);
% Use mesh to draw it
h = mesh([P(:,1) P(:,1)],[P(:,2) P(:,2)],[P(:,3) P(:,3)],C);
set(h,'lineWidth',3);
axis vis3d