Error when creating legend
Mostrar comentarios más antiguos
Hi everyone
I am trying to print my legend on one of my figures as follows
legend('Signal', sprintf('Amplitude = %0.04d', 9.5971e-5 ), sprintf('RMS = %0.004d', 1.2894e-5 )
This prints my amplitude value but completely ignores the RMS value. Is there something I am doing wrong?

1 comentario
does it give you a warning? It looks like you only have 2 things plotted.
For example here I try to legend 2 things but there is only 1 plot so MATLAB gives warning.
figure;
plot(1:3);
legend('test','test2')
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 4 de Mzo. de 2022
Editada: Image Analyst
el 4 de Mzo. de 2022
It only prints as many legend items as there are things that you plotted. Since you only plotted two things, the blue curve and the red circles, only legends for those two items will show up no matter how many strings you feed into legend(). Look at it this way, if it DID plot some line/symbol combination for RMS, what would it use? A green triangle? Why? There is no green triangle plotted nor any other things other than the blue curve and the red circle. Since it can't figure out what to use, it just doesn't use those extra legend arguments.
If you want some other text on there, you can use the text() function. Like
str = sprintf('RMS = %0.9f', 1.2894e-5 )
text(1, -1, str);
Maybe you meant to do this:
data = randn(1,10);
[amp,idx] = max(data);
plot(data);
hold on
plot(idx,data(idx),'o');
legend('Signal', sprintf('Amplitude = %0.04d\nRMS = %0.004d', amp, rms(data)))
1 comentario
David Harra
el 4 de Mzo. de 2022
Categorías
Más información sobre Legend en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



