MATLAB Create Dynamic Legend in MatLab

  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Dynamic Matlab
AI Thread Summary
To create a dynamic legend in MATLAB that updates with each iteration, use a loop to plot your data with specified colors and line styles. Incorporate the iteration variable 'i' into the legend labels by utilizing the sprintf function for formatting. This allows for customized legend entries like '1 legend 1', '2 legend 2', etc., while maintaining consistent colors across different settings. Additionally, consider using a cell array for managing line types. This approach effectively combines dynamic labeling with varied plot styles.
EngWiPy
Messages
1,361
Reaction score
61
Hello,

I am trying to do the following:

for i=1:3
semilogy(plot1,'r(-,:,d)',plot2,'b(-,:,d)',plot3,'k(-,:,d)');
legend('i legend1','i legend2',''i legend1'')
end

where I need to draw the same plots under different settings, such that each function has the same color under the different settings, but with different style, e.g: solid line in the first, dotted in the second, ... and so on. I also want to include the number i in the legend at each iteration. How to do that?

Thanks
 
Physics news on Phys.org
This may be of use: http://www.mathworks.com/help/techdoc/creating_plots/braliom.html
 
Actually, if I'm understanding what you want to do, I think you could get away with using sprintf and format statements as inputs to the legend and semilogy functions.

Something like:
Code:
legend(sprintf('%i legend 1', i), sprintf('%i legend 2', i), sprintf('%i legend 1', i))
It's not pretty but it should work. You can do something similar for the line types with a cell array.
 

Similar threads

Replies
2
Views
3K
Replies
4
Views
2K
Replies
12
Views
3K
Replies
7
Views
3K
Replies
2
Views
15K
Replies
3
Views
3K
Back
Top