make a legend display a variable input
7 ビュー (過去 30 日間)
古いコメントを表示
I'm trying to add a legend in a figure where the input of the legend is variable. I want a plot which displays 4-6 conditions with a legend that displays the corresponding conditions.
The code below is only to illustrate what i want to create. The first for-loop(for plotting) is of no importance.
clc
close all
clearvars
tst = [ 12 21 14 18];
ntst = numel(tst);
for w=1:ntst
hold on
x=linspace(1,100);
y=sin(x+x*tst(w));
plot(x,y,'Color',[rand(1),rand(1),rand(1)])
end
b='';
for a=1:ntst
b=[num2str(tst(a)),' test'];
end
legend(b,'Location', 'southeast')
The actual function has a matrix that contains data for 4-6 conditions and there is no need to plot it in a loop. The text that i want to display in the legend is formatted the same as 'tst' in the code above.
0 件のコメント
回答 (1 件)
Jos (10584)
2016 年 7 月 6 日
編集済み: Jos (10584)
2016 年 7 月 6 日
% an example
x = -10:10 ;
color = {'r','g','b','k'} ;
hold on ;
for k=1:4,
y = (k-2) * x + k ;
ph(k) = plot(x,y,'.-') ;
set(ph(k),'color',color{k}) ;
legendstr{k} = sprintf('%d * x + %d',[k-2 k]) ;
end
hold off
legend(ph,legendstr)
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!