MATLAB: Automating line markers, colors…

automagic line identificationholdplot

When use plot with an array by column, plot automagically cycles colors but when adding a series of lines with hold on after the first the color does not cycle.
One can, of course, create an array and set the colors manually but that becomes old quickly. The question is, then–
Is there a way to simply request the next color and/or marker in the internal list without having to build the arrays to pass? Or, equivalently, is there some combination of properties that can be set that will keep the parts of hold one wants (namely the axes limits, previous lines retained, etc., etc., …) but yet let the color cycle? Of course, ideally the first of simply being able to reference/increment an index in the internal cycle table modulo size of table would be ideal…

Best Answer

This is what I always do:
x = linspace(1,2*pi,250);
v = 1:5;
y = cos(v'*x);
h = colormap(jet(5));
figure(1)
plot(x,y(1,:),'Color', h(1,:))
hold on
for k1 = 2:5
plot(x,y(k1,:), 'Color',h(k1,:))
end
hold off
grid
Slightly cumbersome, but automated and effective. Experiment with the colormap to get the result you want.
You probably have to build the marker array yourself.
NOTE — R2014b now cycles the colours without having to do anything special.