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 CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by