Dynamic Legends plot option (error: two legend lines for each plot)

2 ビュー (過去 30 日間)
Manuel
Manuel 2013 年 9 月 3 日
I have a function which makes a plot from different vectors, then each time that I add a new plot I need to add a new legend which is a variable(legend) now I trying to do it with the next code:
plot_test = plot(vector1, vector2, '-mo','Color',color, 'DisplayName',legend);
hold all;
legend('-DynamicLegend');
but as result I got two new legends for each plot, one whith the real name and another called line2, line4, line6....and so on depending the number of plots...
Any help please?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 3 日
編集済み: Azzi Abdelmalek 2013 年 9 月 3 日
Look at this example
x=0:0.1:10;
y1=sin(x);
plot(x,y1)
hl=legend('leg1')
hold all
y2=cos(x)
plot(x,y2)
leg=[get(hl,'string'), 'leg2']
hl=legend(leg)
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 3 日
Add to your input and output argument of your function the handles old_hl and hl
Manuel
Manuel 2013 年 9 月 4 日
編集済み: Manuel 2013 年 9 月 4 日
This is the function now:
function [plot_test, hl, old_hl] = plot_vector(Test, cond1, cond2, cond3, color, test1, hl, old_hl)
plot_test = plot(cell2mat(plot_array.vector1),cell2mat( plot_array.vector2),'-mo','Color',color);
old_hl=legend(strcat(cond1,'°C ',cond2,'mV ',cond3,'Overshoot'));
leg=[old_hl(hl,'string'), strcat(cond1,'C ',cond2,'mV ',cond3,'Overshoot')];
hl=legend(leg);
hold all
I have the same behavior, I don't understund from where is coming old_hl, there is no variable call like that...
I really appreciatte your help Azzi, Thanks again!

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

その他の回答 (1 件)

Robert Cumming
Robert Cumming 2013 年 9 月 3 日
Is this the type of feature your after:
figure;
x = [0:0.01:2*pi];
hold on;
plot ( x, sin (x), 'DisplayName', 'Plot A' )
legend ( '-dynamicLegend' )
plot ( x,- sin (x), 'r', 'DisplayName', 'Plot B' )
plot ( x, 2*sin (x), 'g', 'DisplayName', 'Plot C' )
  1 件のコメント
Manuel
Manuel 2013 年 9 月 3 日
編集済み: Manuel 2013 年 9 月 4 日
I dont understund what you mean. I was using this option but it creates two legend fields(one as'Plot A' and one as 'line2') for each plot.

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

カテゴリ

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