where to put legend call in nested for loop

1 view (last 30 days)
Katherine
Katherine on 8 Feb 2014
Commented: Katherine on 8 Feb 2014
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 Comments
Amit
Amit on 8 Feb 2014
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 on 8 Feb 2014
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?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!