Need legend for lines plotted with different style in for loop and if statement

1 回表示 (過去 30 日間)
Miraboreasu
Miraboreasu 2023 年 2 月 21 日
コメント済み: Adam Drake 2023 年 3 月 13 日
clear
f=figure;
x = 1:0.01:10; %ft
k = 1:10;
b = 1:10;
cmap = colormap(cool(length(k)));
for ii = 1:length(k)
p1=plot(x,sl(x,k(ii),b(ii)),'-','LineWidth',3,'Color',cmap(ii,:))
hold on
if(ii>5&&ii<7)
p2=plot(x,sl(x,k(ii)+0.1,b(ii)+0.1),'--','LineWidth',3,'Color',"red")
hold on
end
end
hold on
hold off
axis tight
grid on
legend([p1 p2],'1','2','3','4','5','6 special','7','8','9','10','11','Location','eastoutside')
legend show
function y = sl(x,k,b)
y = k*x+b;
end
It only have two legend, need help here, thank you!

回答 (1 件)

Adam Drake
Adam Drake 2023 年 2 月 21 日
編集済み: Adam Drake 2023 年 2 月 21 日
clear
f = figure;
x = 1:0.01:10; %ft
k = 1:10;
b = 1:10;
cmap = colormap(cool(length(k)));
count = 1;
for ii = 1:length(k)
p1(count) = plot(x,sl(x,k(ii),b(ii)),'-','LineWidth',3,'Color',cmap(ii,:));
count = count + 1;
hold on
if( ii > 5 && ii < 7)
p1(count) = plot(x,sl(x,k(ii)+0.1,b(ii)+0.1),'--','LineWidth',3,'Color',"red");
count = count + 1;
end
end
hold off
axis tight
grid on
legend(p1,'1','2','3','4','5','6','6 special','7','8','9','10','Location','eastoutside')
function y = sl(x,k,b)
y = k * x + b;
end
  5 件のコメント
Adam Drake
Adam Drake 2023 年 3 月 13 日
Could you please accept this answer if it answered your problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by