Is it possible to insert in a legend a calculated variable?

Hey everybody,
ts me again with an maybe simple question for you. I have a for loop and calculate some data, which I plot at the end of each loop. Is it possible to define, that the legend will have an calculated data in it? Such as loop one z=1, loop two z=2.5 and so on.
Thanks in advance for some help

 採用された回答

Chandrasekhar
Chandrasekhar 2014 年 4 月 8 日

4 投票

for eg:
z = 1;
%loop1
data1 = [1:10];% data obtained in loop1
z = 2.5;
%loop2
data2 = [1:2.5:25];% data obtained in loop2
plot(data1);
hold
plot(data2,'r');
legend('z = 1','z = 2.5');
xlabel('xaxis');
ylabel('yaxis');
grid on

6 件のコメント

Chris
Chris 2014 年 4 月 8 日
Is there any posibility to set instead of legend('z = 1','z = 2.5'); --> legend('z = '); That the legend automatically knows which ifrom which loop?
Chandrasekhar
Chandrasekhar 2014 年 4 月 8 日
yes.. if
z1 = 1 %in loop1
z2 = 2.5 %in loop2
legend(['Z=' num2str(z1)],['Z=' num2str(z2)]);
Rodrigo Diaz
Rodrigo Diaz 2015 年 9 月 9 日
Thanks Akshata M. It really works!!.
Saeed Ahmadzadeh Mahboobi
Saeed Ahmadzadeh Mahboobi 2019 年 12 月 17 日
Ganz deutlich und kurz erklärt.
Danke Chandrasekhar
Hadj BENZATER
Hadj BENZATER 2020 年 3 月 25 日
thanks
Lukas Guldersson
Lukas Guldersson 2020 年 6 月 3 日
thanks

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

その他の回答 (1 件)

Dishant Arora
Dishant Arora 2014 年 4 月 8 日

17 投票

str = {strcat('z = ' , num2str(z))} % at the end of first loop, z being loop output
str = [str , strcat('z = ' , num2str(z))] % after 2nd loop
% plot your data
legend(str{:})

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by