where to put legend call in nested for loop
2 views (last 30 days)
Show older comments
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
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 ?
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!