Help With Plot Legend

2 views (last 30 days)
Edward on 11 May 2012
if i have a cell array of strings for example:
and make a plot with 3 lines each with variable name cell{n}.
Why is it that writing:
legend(cell{1}, cell{2}, cell{3})
only returns a legend with the one variable name cell{3} in it?
Is there a way of doing this for more than 3 variables using a loop?
Dr. Seis
Dr. Seis on 12 May 2012
I guess the question is... how is your plots set up? Can you copy/paste that part of your code?

Sign in to comment.

Answers (2)

Dr. Seis
Dr. Seis on 11 May 2012
If you are just trying to apply the 3 legend strings to 3 lines on a plot, then all you need to do is:
Take a look at these other posts related to legend entries for more help:

Image Analyst
Image Analyst on 11 May 2012
It doesn't. Here, try this:
colorMap = lines(8)
for k = 1:3
y = rand(1,10);
plot(y, 'Color', colorMap(k,:));
hold on;
legend(cellArray{1}, cellArray{2}, cellArray{3});
But I did notice something very bad in your code. You overwrote the built-in MATLAB function called "cell" with your variable named "cell." Don't do that. Other common functions/entities people destroy are "i", "j", and "image".
Walter Roberson
Walter Roberson on 12 May 2012
IA, my comment was in response to a comment from someone else that has since been deleted.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!