Control color in a for loop
9 views (last 30 days)
Claudius Simon Appel on 20 Mar 2020
this is hopefully the last question I have for now.
The function "LPplotfun" will plot the matrices LPmatx/LPmaty row for row against each other. I want each plotted line to have a different color, so that they're distinguishable. However, I don't want it to be random. Instead, the first function is supposed to be red, second blue, third green, etc. If no colors are left it is supposed to roll over to red again.
The reason is that the function "LPerrorbarfun" (if it is activated) will plot the errorbars for the data points of each plot iteration. In that case, the errorbars and respective graphs should have the same color. So I have to somehow controll which color the "LPplotfun" and "LPerrorbarfun" grab use at each iteration of the for-loop.
I tried experimenting with the "colororder"-command, but couldn't figure out how it works/how I could implement it.
Is there a way to achieve this?
Below is the code for both functions
%% The plotting function
function LPplotfun(LPmatx,LPmaty,LPFits,LPNumRows,LPErrorbar_Flag) %LPplotfun plots all LPfit set up by LPfitfun
axis([-500 500 -500 500])
if LPErrorbar_Flag == 0
end% LPfits=LPfitfun(LPmatx,LPmaty,LPNumRows) %"LPfits" is the name of the cell in which all LPfits are stored.
%% The errorbar function
%LPerrorbars plots all errorbars for the different data points
Ameer Hamza on 20 Mar 2020
[Comment moved to Answer as OP comfirmed that it resolved the issue]
Why not just use the 'color' property of the line object. For example, in the function LPplotfun, before the for loop define
colors = [1 0 0; % red
0 0 1; % blue
0 1 0;]; % green
and change the plot() function call like this
plot(LPmatx(k,:),LPmaty(k,:), 'o', 'Color', colors(rem(k-1,3)+1,:))
rem() will cause the colors to loop back.
Similar solution can be used for errorbar.