Control color in a for loop

7 views (last 30 days)
Claudius Simon Appel
Claudius Simon Appel on 20 Mar 2020
Commented: Ameer Hamza 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?
Thank you.
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
hold on
for k=1:1:LPNumRows
hold on
axis([-500 500 -500 500])
hold on
if LPErrorbar_Flag == 0
hold on
hold on
end% LPfits=LPfitfun(LPmatx,LPmaty,LPNumRows) %"LPfits" is the name of the cell in which all LPfits are stored.
hold on
%% The errorbar function
function LPerrorbars(LPmatx,LPmaty,LPmaterrorx,LPmaterrory,LPNumRows)
%LPerrorbars plots all errorbars for the different data points
hold on
for k=1:1:LPNumRows
hold on
hold on
Ameer Hamza
Ameer Hamza on 20 Mar 2020
Glad to be of help. I have moved my comment to answer, since it solved the issue.

Sign in to comment.

Accepted Answer

Ameer Hamza
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.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!