MATLAB: How to change the line color in an ‘animatedline’ plot in a manner that scales with one of the variables

animatedlinecolor

I'm using the code below to animate a line for the purpose of a presentation. It seems that the 'animatedline' function doesn't support the option of changing color over the course of the plot (e.g., in a manner that scales with X, Y or Z). It seems that the 'patch' function supports this, and I'm wondering if there's a way to do something similar with 'animatedline'? Any help from the Matlab experts?
%X, Y and Z were emperically determined from a separate analysis, and are pre-assigned
view([-12 34])
curve = animatedline('Color','r','LineWidth',3);
numpoints = size(x)
ax = gca;
axTickDir = 'out'
ax.FontSize = 11;
ax.XLim = [0 11];
ax.YLim = [1 5];
ax.ZLim = [1.5 3]
for k=1:numpoints
addpoints(curve,x(k),y(k),z(k));
pause(0.05)
M(k) = getframe(gcf);
end
drawnow

Best Answer

There are other ways to create animation.
You can segement the line and conrol the color of each segment (see "segmentation demo" in this answer).
To animate it, plot it within a loop that iteratively adds each segment.
hold on
for i = 1:number_of_segments
plot(x_segment,y_segment,'-','color', RGB_segment)
drawnow()
pause(.1) % to slow it down, if needed
end
If you get stuck, show what you tried and I'd be glad to work out the kinks.
Alternatively, you could use some undocumented approaches to changing line color of a line object. See Yair's blog post below. https://undocumentedmatlab.com/matlab/wp-content/cache/all/articles/plot-line-transparency-and-color-gradient/index.html