필터 지우기
필터 지우기

How do I create a legend and include loop variable values in the labels while plotting a Matrix ?

조회 수: 6 (최근 30일)
u = 1:2:10;
z_x = 1:10;
temp = [1:10;11:20;21:30;31:40;41:50];
figure,
hold on
for l= 1:length(u)
str = ['S = ',num2str(u(l))];
plot(temp,z_x,'DisplayName',str);
end
legend show
The above code displays one value of S multiple times. Since i'm plotting a matrix and want to produce a legend for each uniques curve only one. What correction should I make so that the each S value is displayed only once in the legend.
  댓글 수: 4
AKRITI KOTHIALA
AKRITI KOTHIALA 2020년 6월 19일
편집: AKRITI KOTHIALA 2020년 6월 19일
Thanks for your response. This code produces k =1 multiple times then k=2 another multiple times and so on...in the legend. I want it to give k=1,k=2...,one time only, corresponding to each curve.
AKRITI KOTHIALA
AKRITI KOTHIALA 2020년 6월 19일
편집: AKRITI KOTHIALA 2020년 6월 19일
u = 1:2:10;
z_x = 1:10;
temp = [1:10;11:20;21:30;31:40;41:50];
figure,
hold on
for l= 1:length(u)
str = ['S = ',num2str(u(l))];
plot(temp,z_x,'DisplayName',str);
end
legend show
I agree the above code is very messy. Pehaps this code will explain my problem more clearly.

댓글을 달려면 로그인하십시오.

채택된 답변

madhan ravi
madhan ravi 2020년 6월 19일
m = rand(10,8);
plot(m)
legend(compose('s = %d', 1 : size(m,2)))

추가 답변 (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