How to add a legend to a figure, but a legend that changes every iteration in a loop ?
Mostrar comentarios más antiguos
I have a "while" loop (while i< tmax ). What i would like to do is to add the element "curve -- i " to the legend every iteration "i". For example if "i" goes from 1 to 3, the legend will be: curve1 curve 2 curve 3. If "i" goes from 1 to 2, the legend will be only: curve1 curve 2. Is it possible to ensure this flexibility in the legend?
Respuesta aceptada
Más respuestas (2)
Sergio Lobo
el 29 de Jul. de 2017
I found this modification to work better:
j = 1;
leg = {'a', 'b', 'x'};
figure()
hold on
while j < 4
plot(rand(1,20))
curr(j) = leg(j)
legend( curr);
j = j+1;
pause(3);
end
hold off
Walter Roberson
el 7 de Ag. de 2015
You can use sprintf() to construct the strings that you legend() . For example,
i = 0;
residue = sqrt(2);
while residue > 0.1
i = i + 1;
h(i) = plot(rand(1,20));
hold on
legend(h(i), sprintf('curve %d', i));
residue = residue - rand();
end
1 comentario
bh dhouha
el 7 de Ag. de 2015
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!