where to put legend call in nested for loop

조회 수: 1 (최근 30일)
Katherine
Katherine 2014년 2월 8일
댓글: Katherine 2014년 2월 8일
I'm trying to loop through some statistics, months, files, and then years. What I want is to have one plot per month for each statistic with all of the files and years. I would like it to run through one statistic, and for each month, grab the files and the years, so if there are two years than both of the Jan yr 1 and Jan yr 2 would be on the same plot. What I'm wondering is where to define the empty legend tag and then where to put my legend call within the end statments of my for loop to get the desired result. This is what I have but it doesn't seem to be working exactly how I want it:
for ms=1:length(stats)
legendms={};
for m = 1:12
figure
for i = 1:length(files)
year=unique(files(i,1).allData{1});
for y = 1:length(files(i,1).year)
monthString = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
mString= monthString(1:length(files(i,1).year(y).month));
yString=cellstr(num2str(year));
semilogx(xvals,files(i,1).year(y).month(m).(x{ms}),LineStyles{y}, 'Color', LineColors{m}, 'LineWidth',lWidth)
legendms{end+1} = [files(i,1).sta ' ' files(i,1).loc ' ' files(i,1).comp ' ' monthString{m} ' ' yString{y}(3:4)];
end
end
semilogx(nlnmx,nlnmy,'k-','LineWidth',lWidth)
semilogx(nhnmx,nhnmy,'k-','LineWidth',lWidth)
hold off
legend([legendms 'NLNM', 'NHNM'], 'Location', 'EastOutside')
end
end
Can anyone suggest where the legendms={} should be? Do I have my figure call in the right place? Is my legendms{end+1} variable defined in the correct loop? What about when I call the legend itself?
Thanks for any help with this issue!
  댓글 수: 2
Amit
Amit 2014년 2월 8일
Lets say there are 2 plots for 2 years, what should be your legend for the case Jan? Can you simply write the legend text for a sample case ?
Katherine
Katherine 2014년 2월 8일
So say we looped through the first stat, ideally I would loop through all of the months and files (stations) and years. So let's say there are 12 months, 2 years, and 3 stations, then for each month there would be a plot. Here's what I would like the legend to be for stat1, month of January, all files, and all years, i.e., figure 1:
sta1 Jan 12
sta1 Jan 13
sta2 Jan 12
sta2 Jan 13
sta3 Jan 12
sta3 Jan 13
The plot for that stat and all of the stations for the month of January would be on that figure. Make sense? Or should I explain further?

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

답변 (0개)

카테고리

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