Hi, my task would be to have a legend which is update dynamically as I loop over some data extracted from different files and I add them to a same plot, for instance. To put it better, I would like to "append" a label to my legend every time I update my plot. Would you know how to do it?
Chiara

4 件のコメント

John Bailey
John Bailey 2018 年 5 月 12 日
編集済み: John Bailey 2018 年 5 月 12 日
plot(Pss,'DisplayName','desired legend entry')
legend('-DynamicLegend')
Place that after your first plot and it will automically put the name of the plot variable into the legend.
Walter Roberson
Walter Roberson 2018 年 5 月 12 日
Note that dynamic legends did not exist at the time the original question was asked in 2011.
Matthew
Matthew 2018 年 9 月 25 日
Those were the days.
Moritz Stich
Moritz Stich 2022 年 3 月 31 日
Jon Bailey you are a dynamic legend. Thanks

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

 採用された回答

Paulo Silva
Paulo Silva 2011 年 3 月 21 日

1 投票

clf
clc
hold all
x=0:0.01:2*pi;
b=zeros(5);
for a=1:5
b(a)=plot(x,sin(a*x));
legend(b(1:a))
pause(1) %just to see it being draw
end
Alternative with choosen names for each plot
clf
clc
hold all
x=0:0.01:2*pi;
b=zeros(5);
c={'one','two','three','four','five'};
for a=1:5
b(a)=plot(x,sin(a*x));
legend(b(1:a),c(1:a))
pause(1)
end

1 件のコメント

Chiara Modenese
Chiara Modenese 2011 年 3 月 21 日
Thanks for a very helpful community. Question is answered.
Cheers. Chiara

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

その他の回答 (2 件)

Matt Fig
Matt Fig 2011 年 3 月 21 日

0 投票

How are you updating the plot? In a loop?

1 件のコメント

rodolfo reyesbaez
rodolfo reyesbaez 2019 年 5 月 21 日
I added a hold on before ending the loop

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

Chiara Modenese
Chiara Modenese 2011 年 3 月 21 日

0 投票

Yes, in a simple loop. Data come from different structures, but this is not so important. Basically, I want to avoid this:
legend(ss(1).name,ss(2).name,ss(3).name);
where ss(i) are structures. Every time I run my code, I can chose the number of structures to create, so I do not want to modify my legend as a function of this but, instead, do it automatically (so if I have 5 structures, I expect 5 corresponding labels in my legend, no need to modify the code by hand).
Thanks, Chiara

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by