Doubt with plot legend

I want to plot a graph consisting of ten different sets of data of the same entity, measured in 10 different initial conditions. I have all the data I need, and I am able to plot the graphs and select color and line styles as I want, but when I try to include a legend in the plot, it gets a bit tricky. The goal is to be able to have a dynamic legend, that automatically includes all the ammount of curves I want to plot, in case these should change number over time.
For the time being, I'm plotting 10 curves, so saying legend(1,2,3,4,5,6,7,8,9,10) is simple. But what if I want MATLAB to recognize the number of plots I want to make, identify each of the different names, and then legend the curves accordingly? What do I need to know in order to do that?
Thank you.

 Accepted Answer

Image Analyst
Image Analyst on 7 Apr 2015

0 votes

Use a cell array to make up a custom list of strings you want displayed in the legend. I don't know what you want each one to be but you might be able to use sprintf().

8 Comments

Ok, let me try and be more specific. Assume I have the data of 10 different load cases, being 100, 200, 300, 400, 500, 600, 700, 800, 900 and 1000 Newtons each one of the cases I need to plot. I want the plot to contain all 10 curves, and I want the legend to show each the correct number, and the 'N' unit, right next to it. So, my question really would be how to create a 2 dimension matrix where the 1st column are numbers, and the 2nd are strings? My attempts didn't work so well, so far. It is just a simple question, but it could help me a lot to reduce time in my work.
Thank you again.
This is what I was trying to say. Is it what you want?
% Create sample data
loadCases = rand(10, 12);
% Separate them vertically to make them easier to see.
for row = 1 : size(loadCases, 1)
loadCases(row, :) = loadCases(row, :) + row;
end
% Now that we have data, let's plot it.
for row = 1 : size(loadCases, 1)
thisLoad = loadCases(row, :);
plot(thisLoad, '-', 'LineWidth', 3);
hold on;
% Make up legend entry for this row.
legends{row} = sprintf('%d', row*100);
end
grid on;
% Put up the legends.
legend(legends);
If not, maybe you can alter the sprintf() line to get what you want.
Vicente, you must have a version of MATLAB older than R2014b. In those versions the plots all came out in blue if you didn't specify the color. In R2014b and later, they show up in different colors (which you can control or override if you want).
Don't worry about my data being spaced vertically. I just made up some arbitrary data to make it easy to see. You should use your own, actual data.
If you have some number and want it to go into a legend with double quotes and some color name, just use sprintf() like before. For example
legends{row} = sprintf('“yellow line” %d N', number)
The legend will be: “yellow line” 400 N
Thank you so much!
I managed to create the graph I wanted. I attatched a printscreen of the plot. But now, what if I want the legend to contain only 3, 4 digits, what should I do? I tried including a line format short before all the plot commands, but it didn't work. Another question, this is not the only plot that I need to make. Is there a command in MATLAB that allows me to generate more than one plot within one simulation? Like, different plot windows, not all compacted in the same, like subplots, but multiple windows like the one I just did?
It looks like you're using %f when you're creating the legend strings. Try putting in field widths and number of decimal places, like %7.3f or %11.3g or whatever looks good and consistent.
The reason why I used %f as it was in your code, is because I really don't have a clue what does this designation do. What is %f, %7.3f, %11.3g? How are they supposed to be used? I tried typing "help %f" on command window, but as it's not a command, it didn't show any results.
Look up sprintf() in the help
MATLAB -> Language Fundamentals -> Data Types -> Characters and Strings -> Create and Concatenate Strings -> sprintf
There is a discussion of them there.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!