I have created an array of colors to loop over when outputting different plots.
Somewhere in the code it is broken. I am getting no errors, but yet the colors output does not represent the color array. It outputs the last color in the array for all lines in the color black.
figure1 = figure('Color','w');
apples = [3 5 6 3 2]
oranges = [2 3 4 5 6]
grapes = [3 4 3 2 5]
count = [1 2 3 4 5]
C = {'b','r','g','m','k'}
hold on
for i = 1:5
line(count, apples, 'LineStyle','-','LineWidth', 2, ...
'Marker','x', 'MarkerSize',10, 'Color',C{i},'DisplayName','apples');
line(count, oranges, 'LineStyle','-', 'LineWidth', 2, ...
'Marker','.', 'MarkerSize',10, 'Color',C{i},'DisplayName','oranges');
line(count, grapes, 'LineStyle','-', ...
'Marker','.', 'MarkerSize',10, 'Color',C{i},'DisplayName','grapes' );
end
hold off

Issue:
As alluded to in my comment, your index i ranges from 1 to 5 and inside said loop you plot three lines each time (15 total lines). Each iteration is changing plotting three lines with the same color (first blue, then red, and so on).
Solution:
General approach is to separate the plot and the plot properties in organized way that automates desired portions.
Automate color: Code below uses a matrix of RGB values called Cmat.
Common line properties: Common plot properties (all lines) adjusted with for loop.
Individual line properties: Specific line properties adjusted via switch statement.
I generally like to leave the DisplayName with the plot call just for readability but that's my preference, especially if variable names are more abstract.
% MATLAB R2017a
figure1 = figure('Color','w');
hold on
apples = [3 5 6 3 2];
oranges = [2 3 4 5 6];
grapes = [3 4 3 2 5];
count = [1 2 3 4 5];
% C = {'b','r','g','m','k'}
Cmat = [0 0 1; 1 0 0; 0 1 0; .8 0 .2; 0 0 0]; % use a matrix of RGB values
p(1) = plot(count,apples,'DisplayName','apples') % give plots handles
p(2) = plot(count,oranges,'DisplayName','oranges')
p(3) = plot(count,grapes,'DisplayName','grapes')
% Cosmetics
for j = 1:3
p(j).LineStyle = '-'; % Common plot property adjustments
p(j).LineWidth = 2;
p(j).MarkerSize = 10;
p(j).Color = Cmat(j,:);
switch j % Specific plot property adjustments
case 1
p(j).Marker = 'x'; % Marker type for 1st line
case 2
p(j).Marker = '.';
case 3
p(j).Marker = '.';
end
end
% legend('show') % include legend
Undoubtedly there are more efficient solutions. I encourage OP and future readers to also see this excellent answer from @gnovice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With