Create Dynamic Legend in MatLab

  • Context: MATLAB 
  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Dynamic Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a dynamic legend in MATLAB for a series of plots generated in a loop. Participants explore how to maintain consistent color coding while varying line styles and incorporating loop iteration numbers into the legend entries.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their attempt to plot multiple functions with different line styles and colors while wanting to include the loop index in the legend.
  • Another participant expresses uncertainty about the original request but references a related thread that might provide insight.
  • A third participant shares a link to MATLAB documentation that could be relevant to the plotting functions being discussed.
  • A later reply suggests using the sprintf function to format the legend entries dynamically, proposing a method to achieve the desired outcome with formatted strings.
  • There is a mention of using a cell array for line types, indicating a potential approach to manage different styles in the plots.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best method to implement the dynamic legend, as multiple suggestions and interpretations of the original question are presented.

Contextual Notes

Some assumptions about the desired output and the specific requirements for the legend formatting remain unclear, and the discussion does not resolve these ambiguities.

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 ·
Replies
2
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K