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
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');

  0 Comments

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.
legend(strcat('p=',num2str(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);
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 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)
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.

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 17 Jul 2014
To concatenate a string with a number
number=10
out=sprintf('SNR%d',number)

  2 Comments

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),...,
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!