Insert legend in a for loop
조회 수: 16 (최근 30일)
이전 댓글 표시
I'm producing a figure inside a for loop and would like to insert a legend. The script for the figure is as follows:
Name={'Data1','Data2','Data3'};
cmap = hsv(length(fieldnames(Data)));
str = 'LWST : ';
for i=1:length(fieldnames(Data));
plot(Data.(Name{i}),'Color',cmap(i,:));hold on
end
So, in order to insert a legend I include the following command in the loop:
legend(Name(i));
But the legend just overlies the previous one. So, instead of having a legend showing the 3 datasets it only shows one! what should I do?
댓글 수: 0
채택된 답변
Daniel Shub
2012년 1월 10일
Move your legend command outside the loop ...
for i=1:length(fieldnames(Data));
plot(Data.(Name{i}),'Color',cmap(i,:));hold on
end
legend(Name);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!