필터 지우기
필터 지우기

Help With Plot Legend

조회 수: 2 (최근 30일)
Edward
Edward 2012년 5월 11일
hello,
if i have a cell array of strings for example:
cell{1}='st1';
cell{2}='st2';
cell{3}='st3';
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?
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 5월 11일
Duplicate is at http://www.mathworks.com/matlabcentral/answers/38113-plot-help
Dr. Seis
Dr. Seis 2012년 5월 12일
I guess the question is... how is your plots set up? Can you copy/paste that part of your code?

댓글을 달려면 로그인하십시오.

답변 (2개)

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

Image Analyst
Image Analyst 2012년 5월 11일
It doesn't. Here, try this:
cellArray{1}='st1';
cellArray{2}='st2';
cellArray{3}='st3';
colorMap = lines(8)
for k = 1:3
y = rand(1,10);
plot(y, 'Color', colorMap(k,:));
hold on;
end
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".
  댓글 수: 2
Image Analyst
Image Analyst 2012년 5월 12일
I guess I don't get it. You could call plot() 3 times with different variables and call legend and have all variable names show up in the legend. You could make 3 arrays, st1, st2, and st3, and call plot(st1), plot(st2), and plot(st3) and then call legend and have 3 lines with each name beside a line in the legend.
Walter Roberson
Walter Roberson 2012년 5월 12일
IA, my comment was in response to a comment from someone else that has since been deleted.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by