MATLAB Answers

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

832 views (last 30 days)
Tien T
Tien T on 17 Jul 2014
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
legend('SNR =',num2str(SNR),...'location','Best');


Sign in to comment.

Answers (3)

Jayanth Reddy Regatti
Jayanth Reddy Regatti on 11 Sep 2016
Edited: Jayanth Reddy Regatti on 11 Sep 2016
If this question is still not solved, here is one work around. Declare your Snr values in a column vector Pr.
This works if you are plotting all the curves at a go.

  1 Comment

Marina Ramos Cuevas
Marina Ramos Cuevas on 11 May 2020
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!

Sign in to comment.

Chad Greene
Chad Greene on 6 Aug 2014
Edited: Chad Greene on 6 Aug 2014
I am having trouble understanding your question. Perhaps legappend is the ticket?
x = 1:.1:3;
y = sin(x);
hold on;
colors = jet(10);
for n = 1:10
s = rand;

  1 Comment

Tien T
Tien T on 8 Aug 2014
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)
legendInfo = ['SNR = ' num2str(increP) ' dB'];
increP = increP+1;
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.

Sign in to comment.

Azzi Abdelmalek
Azzi Abdelmalek on 17 Jul 2014
To concatenate a string with a number


Tien T
Tien T on 6 Aug 2014
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 on 6 Aug 2014
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),...,
'DisplayName',sprintf('SNR = %d',increP));
increP = increP+1;
I'm not really sure how else to auto increment a number inside a string and display them on a legend.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!