Create Dynamic Legend in MatLab

In summary, the conversation discusses how to draw multiple plots with different settings and styles, while also including a specific number in the legend at each iteration. The use of sprintf and format statements is suggested as a potential solution.
  • #1
EngWiPy
1,368
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
  • #3
This may be of use: http://www.mathworks.com/help/techdoc/creating_plots/braliom.html
 
  • #4
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.
 
  • #5
for your question!

To create a dynamic legend in MatLab, you can use the "legend" function with the "sprintf" command to include the variable "i" in the legend at each iteration. Here is an example of how you could modify your code to achieve this:

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

This will create a legend with the format "i legend1", "i legend2", "i legend3" for each iteration, where "i" will be replaced with the current value of the variable. You can also use the "sprintf" command to customize the style of the legend entries, such as using different colors or line styles for each iteration. For more information on how to use the "legend" and "sprintf" commands, you can refer to the MatLab documentation or seek further assistance from the MatLab community. Good luck with your project!
 

1. How do I create a dynamic legend in MatLab?

To create a dynamic legend in MatLab, you can use the legend function. This function allows you to specify the labels and the location of the legend. You can also use the num2str function to convert numerical values into strings for legend labels.

2. Can I customize the appearance of my dynamic legend?

Yes, you can customize the appearance of your dynamic legend by using the legend function with additional parameters. These parameters include the font size, font style, color, and position of the legend.

3. How do I add additional items to an existing dynamic legend?

You can add additional items to an existing dynamic legend by using the hold on command before plotting the new data. This will prevent the previous legend from being overwritten. Then, use the legend function with the 'add' parameter to add the new legend items.

4. Can I create a dynamic legend for multiple plots?

Yes, you can create a dynamic legend for multiple plots by using the legend function with the 'plot' parameter. This will create a separate legend for each plot, with the specified labels and location.

5. How can I update the labels on my dynamic legend?

To update the labels on your dynamic legend, you can use the legend function with the 'update' parameter. This will allow you to change the labels of existing legend items or add new items to the legend.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
508
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
Back
Top