How to create a legend for a variable used as for loop iteration?
59 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (3)
Jayanth Reddy Regatti
el 11 de Sept. de 2016
Editada: Jayanth Reddy Regatti
el 11 de Sept. de 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 comentario
Marina Ramos Cuevas
el 11 de Mayo de 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!
Chad Greene
el 6 de Ag. de 2014
Editada: Chad Greene
el 6 de Ag. de 2014
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
Azzi Abdelmalek
el 17 de Jul. de 2014
To concatenate a string with a number
number=10
out=sprintf('SNR%d',number)
2 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!