Azzera filtri
Azzera filtri

How can I create a matrix of linestyles for plot to loop through?

17 visualizzazioni (ultimi 30 giorni)
So, I have this:
linedex={'-','--','-.',':'};
x=[-2;-3;-4];
for i=1:4
mmm= [0.0803 0.1912 0.1245
0.0239 0.3246 0.1581
0.0164 0.5584 0.2294]; %%%For each loop it gets a different set of values, this is and example one, with the columns being min,max,mean
hold on
plt=plot(x,mmm)
plt.LineStyle(i)=linedex(i);
end
I've also tried plotting them individually, where its the same linedex and loop replacing mmm with three vectors instead.
x=[-2 -3 -4]
min=[0.0803 0.0239 0.0164]
max=[0.1912 0.3246 0.5584]
mean=[0.1245 0.1581 0.2294]
plot(x,min,x,max,x,mean)
but then how would I call to those specific lines, to have them all be one style, while the next set of min,max, and mean are in a new style/and then colors eventually?

Risposta accettata

Stephen23
Stephen23 il 26 Lug 2018
Modificato: Stephen23 il 26 Lug 2018
You were almost there, you just need to use the right kind of cell array indexing:
C = {'-','--','-.',':'};
x = [-2;-3;-4];
for k = 1:numel(C)
% Simpler:
mmm = k+[0.0803,0.1912,0.1245;0.0239,0.3246,0.1581;0.0164,0.5584,0.2294];
plot(x,mmm,C{k})
% OR alternatively
%plt = plot(x,mmm);
%plt.LineStyle = C{k};
hold on
end
Which produces this:

Più risposte (1)

Sushant Mahajan
Sushant Mahajan il 25 Lug 2018
Modificato: Sushant Mahajan il 25 Lug 2018
You can use a combination of eval and sprintf. Here I am plotting successive powers of sin(x).
linestyles={'-','--','.-'};
x=linspace(0,90,100);
for i=1:3
eval(sprintf('plot(x,sind(x).^%d,''%s'')',i,cell2mat(linestyles(i))))
hold on
end
Hope this helps!
  2 Commenti
Stephen23
Stephen23 il 26 Lug 2018
Modificato: Stephen23 il 26 Lug 2018
Why on earth do you use eval for this? This is bad advice: eval is totally unnecessary, and pointlessly teaches bad habits to other beginners: using eval for trivial code like this is how beginners force themselves into writing slow, complex, buggy code that is hard to debug. Avoid using this code. Read this to know why:
The code is simpler, neater, and more efficient without eval:
C = {'-','--','.-'};
x = 0:90;
for k = 1:numel(C)
plot(x,sind(x).^k,C{k})
hold on
end

Accedi per commentare.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by