Legend in for loop of unknown number

5 ビュー (過去 30 日間)
bahar
bahar 2014 年 6 月 3 日
編集済み: bahar 2014 年 6 月 3 日
Hi,
I have a problem.
I want to plot a figure in for-loop of unknown number of values.
*********************************
%%%%plot
cc=hsv(nk12*nk21);
jj=1;
for kp=1:nk21
for jp=1:nk12
loglog(p,intensity1(:,jp,kp),p,intensity2(:,jp,kp),'color',cc(jj,:))
ylabel('f^es');
xlabel('t');
title('Graph of intensity for modes 1 and 2')
set(gca, 'XScale', 'log', 'YScale', 'log')
jj=jj+1;
hold all
[~,~,~,names] = legend;
legend([names {sprintf('%s%g','k12,k21=',jp,',',kp)}]);
end
end
*******************************************
How can I arrange this so it plots a legend for each dataset?

回答 (2 件)

Roger Wohlwend
Roger Wohlwend 2014 年 6 月 3 日
First: your code is unreadable if you do not format it as code!
Second: As some variables are missing I cannot run the code, so I cannot guarantee that my answer really works. Try the following:
h = NaN(nk21*nk12,1);
hold on;
for kp=1:nk21
for jp=1:nk12
h(jj) = loglog(p,intensity1(:,jp,kp),p,intensity2(:,jp,kp),'color',cc(jj,:)) ;
jj=jj+1;
end
end
legend(h, s,u,'Location','southeast')
  1 件のコメント
bahar
bahar 2014 年 6 月 3 日
i changed my program. could you please take a lool at it...

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


bahar
bahar 2014 年 6 月 3 日
編集済み: bahar 2014 年 6 月 3 日
this is my plot... i have two curves with two color, red and blue but in legend just show red one.

カテゴリ

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