フィルターのクリア

Automatic change of legend's arguments

1 回表示 (過去 30 日間)
Sim
Sim 2022 年 10 月 5 日
コメント済み: Les Beckham 2022 年 10 月 5 日
Let's consider this example:
labels = {'A','B','C'};
hold on
for i = [1 2 3]
h(i) = plot(rand(10,1));
end
legend([h(1),h(2),h(3)],labels);
Now, I would like to plot only the first and the second plots, i.e.
hold on
for i = [1 3]
h(i) = plot(rand(10,1));
end
legend([h(1),h(3)],labels);
Is there any automatic way to change the legend from
legend([h(1),h(2),h(3)],labels);
to
legend([h(1),h(3)],labels);
?
Basically, I just want to change the index of the plots
i = [1 3]
and get the correct labels.

採用された回答

Les Beckham
Les Beckham 2022 年 10 月 5 日
編集済み: Les Beckham 2022 年 10 月 5 日
labels = {'A','B','C'};
for i = [1 3]
h(i) = plot(rand(10,1), 'DisplayName', labels{i});
hold on
end
grid on
legend
  2 件のコメント
Sim
Sim 2022 年 10 月 5 日
cool, thanks a lot!
Les Beckham
Les Beckham 2022 年 10 月 5 日
You are quite welcome.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by