How do I set the plot color and line style of several data sets using a cell array?

I am plotting several sets of data on a single plot and would like to control the color and linestyle of each line using a cell array like this:
figure(1)
hold on
grid on
xlim([0 60])
colorVec = {'b', 'r', 'y', 'p', 'g', 'c'};
linVec = {'--', '-.','--', '-.','--', '-.'};
yyaxis left
plot(tvar,Ts1(:,:),'Color',colorVec{:},'LineStyle',linVec{:},'LineWidth',2,'MarkerSize',5)
hold off
However, the above code does not work and produces an error. Can anyone help me with the syntax of what I am trying to accomplish? Thank You in advance.

Answers (1)

figure(1)
hold on
grid on
xlim([0 60])
colorVec = {'b', 'r', 'y', 'm', 'g', 'c'};
linVec = {'--', '-.','--', '-.','--', '-.'};
yyaxis left
for i = 1:6
plot(tvar,Ts1(:,i),'Color',colorVec{i},'LineStyle',linVec{i},'LineWidth',2,'MarkerSize',5)
end
hold off

3 Comments

Hi KSSV, Thank you for your response.
I see that it's possible to loop through colorVec and linVec using a for loop. However, I'm wondering if it's possible to loop through the colorVec array without explicitly calling it out in a for loop using a colon? Thank you in advance.
Was this question ever solved? I'm trying to do the same thing...
Not sure, if this helps and it has been a while, but if you set default colors ahead of time, you do not need to call them in the loop - they will be picked automagically in the order they are listed.
set(groot,'defaultAxesColorOrder',[1 0 0;0 1 0;0 0 1])
This is done any time before you hold figure and do the loop.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 23 Apr 2018

Commented:

on 17 May 2019

Community Treasure Hunt

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

Start Hunting!