How to create a legend for a variable used as for loop iteration?

13 ビュー (過去 30 日間)
Tien T
Tien T 2014 年 7 月 17 日
Greetings,
I am looking for a simple way to create a legend that defines a key for every loop iteration of a variable that is used as a for loop instrumentation. Is it even possible to call the loop iteration values without assigning them to an array?
For example, I want to display a variable called SNR and have it automatically increment its value corresponding to its iterations. The legend would look something like this:
-- SNR = 0
-+ SNR = 1
== SNR = 2
... and so on
I saw some previous solutions for this, but can it be done simply without an additional for loop and within 10 lines of code like this?
for SNR = 0:10
% body
end
figure
plot(x,y)
legend('SNR =',num2str(SNR),...'location','Best');

回答 (3 件)

Jayanth Reddy Regatti
Jayanth Reddy Regatti 2016 年 9 月 11 日
編集済み: Jayanth Reddy Regatti 2016 年 9 月 11 日
If this question is still not solved, here is one work around. Declare your Snr values in a column vector Pr.
legend(strcat('p=',num2str(Pr')))
This works if you are plotting all the curves at a go.
  1 件のコメント
Marina Ramos Cuevas
Marina Ramos Cuevas 2020 年 5 月 11 日
Thank you very much! This one actually solved my problem on how not to overwrite the legend fields every time I had to perform a loop operation with a plot inside and I could access the plot 'Display Name' tag itself. Really useful!

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


Chad Greene
Chad Greene 2014 年 8 月 6 日
編集済み: Chad Greene 2014 年 8 月 6 日
I am having trouble understanding your question. Perhaps legappend is the ticket?
x = 1:.1:3;
y = sin(x);
plot(x,y,'linewidth',2);
hold on;
legend('sin(x)')
colors = jet(10);
for n = 1:10
s = rand;
plot(x,y+s,'color',colors(n,:))
legappend(['sin(x)+',num2str(s)])
end
  1 件のコメント
Tien T
Tien T 2014 年 8 月 8 日
Sorry, I have a tough time explaining this. The problem that I can't figure out is how to make a legend that will display the values of my SNR curves so that I can change the range of the loop and the legend will adjust to it.
Your method is very similar to the solution that I am looking for. Instead of using legappend(), I had to initialize a variable and then use legend() to produce the same result. However, I want to indicate each curve as the number from the for loop that I am running.
increP = 0;
for increN = 1:thres_range:thres_range*(SNR_dB+1)
loglog(probFAARR(increN:thres_range),probDetnARR(increN:thres_range));
legendInfo = ['SNR = ' num2str(increP) ' dB'];
increP = increP+1;
end
legend = legend(legendInfo);
The probFAARR and probDetnARR arrys are [836x11] while thres_range = 76 and SNR_dB = 10. The command window shows that there is an error for the last line you see, but there is another error that states 'Index exceeds matrix dimensions.' and doesn't tell me which line that error is on.

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


Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 17 日
To concatenate a string with a number
number=10
out=sprintf('SNR%d',number)
  2 件のコメント
Tien T
Tien T 2014 年 8 月 6 日
Thanks for the response. I understand how to concatenate a string with a number using disp(),sprintf(),fprint(). But how do you implement that and increment the string in a for loop for each curve? Especially when you want to display the string in a legend on a plot?
Tien T
Tien T 2014 年 8 月 6 日
My code looks something like this below but it shows the error, 'Improper assignment with rectangular empty matrix.'
increP = 0;
for increN = 1:thres_end-thres_start+1:(thres_end-thres_start+1)*(SNR_dB+1)
x(increN) = plot(probFAARR(increN:thres_end-thres_start+1),...,
probDetnARR(increN:thres_end-thres_start+1),...,
'DisplayName',sprintf('SNR = %d',increP));
increP = increP+1;
end
I'm not really sure how else to auto increment a number inside a string and display them on a legend.

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

カテゴリ

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