Question

I'm trying to plot some monthly statistics that span over 2 years, where 1 year has only one month of data and the other has 11 months of data. Where I'm having trouble is in the legend label, when I am filling out the label it takes the one month of data from the first year and puts behind the next years labels, as if it is filling in that month. Here's what I mean:

    legendms=['PIC2 -- HHN Jan 13', 'PIC2 -- HHN Feb 13', PIC2 -- HHN March 13', 'PIC2 -- HHN Apr 13', 'PIC2 -- HHN May 13', 'PIC2 -- HHN June 13', 'PIC2 -- HHN July 13', 'PIC2 -- HHN Aug 13', 'PIC2 -- HHN Sept 13', 'PIC2 -- HHN Oct 13', 'PIC2 -- HHN Nov 13', 'PIC2 -- HHN Dec 12']

Where 13 is the 2 digit representation for 2013, and 12 for 2012. It does this even though I am cycling through the years in order. I believe it is because legendms for year 2012 is essentially empty cells except for Dec, and so it's just filling in the space. It is also not following the appropriate line styles either. It labels Jan 13 a different line style '-' than the others when Dec 12 should be the different line style. Does anyone know a way around this, or how to ignore the empty cells while still retaining a full legend label?

Here's the code I have so far, perhaps I am just making a simple mistake? Any help would be greatly appreciated!

close all;
  for ms=1:length(stats)
    legendms=[];
     for i = 1:length(files)
       figure
        for y = 1:length(files(i,1).year)
     if (files(i,1).comp(1) == 'H')
       xvals = files(i,1).allData{7}(1:114);
     elseif (files(i,1).comp(1) == 'B')
       xvals = files(i,1).allData{7}(1:95);
     end
        for m = 1:length(files(i,1).year(y).month)
  %Set up generic month string
  if ~isempty(files(i,1).year(y).month(m).(x{ms}))
    monthString = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
    %Set up month string particular to this dataset 
    mString= monthString(1:length(files(i,1).year(y).month));
    yString=cellstr(num2str(year));
    %Set up axis variable particular to this dataset
    axisVar = [1 length(mString) -70 10];
    LineColors={rgb('red') rgb('blue') rgb('green') rgb('indigo') rgb('cyan')   
    rgb('orange')  rgb('HotPink') rgb('DarkGray') rgb('purple') rgb('BurlyWood') 
    rgb('yellow') rgb('Salmon')};

    %Loop through stats to plot 
 semilogx(xvals,files(i,1).year(y).month(m).(x{ms}),LineStyles{y}, 'Color', LineColors{m},
 'LineWidth',lWidth)
 hold on

 %Set axis interval
 axis([0.1, 172, -190, -80]);

 %Set the axis label max, min, and interval for the Yaxis
  set(gca,'YTick', [-190:10:-80])

  %Label x and y axis and create cell for legend for each sta, loc, chan per
  %file to use below. Make title for each plot
  xlabel('Period (s)','FontSize',labelSize);
  ylabel('Power (dB)','FontSize',labelSize);
  legendms{m} = [legendms ' ' files(i,1).sta ' ' files(i,1).loc ' ' files(i,1).comp ' '  
  monthString{m} ' ' yString{y}(3:4)];

  title([x{ms} ' ' 'Monthly Comparisons', ' ' num2str(files(i,1).allData{2}(1)) '/' 
  num2str(files(i,1).allData{3}(1)) '/' num2str(files(i,1).allData{1}(1)) '-' 
  num2str(files(i,1).allData{2}(end)) '/' num2str(files(i,1).allData{3}(end)) '/' 
  num2str(files(i,1).allData{1}(end))],'FontSize',titleSize);

    end 
  end
end

   %Plot NLNM and NHNM models and label legend for each figure

    semilogx(nlnmx,nlnmy,'k-','LineWidth',lWidth)
    semilogx(nhnmx,nhnmy,'k-','LineWidth',lWidth)
    hold off
    legend([legendms, 'NLNM', 'NHNM'], 'Location', 'EastOutside')
    end
   end

To reiterate I would like the legend to ignore the empty cells for any of the years and plot the legend label in order from the years using the appropriate colors and station labels. 2012 values should be a solid line and 2013 values a dashed line, with whatever colors are representative of the month they describe (lineColors).

Was it helpful?

Solution

You are incrementing legendms with the variable "m," which loops through all the files, even when they are empty. However, you are only plotting using semilogx when the the file is not empty.

Either:

Create a new variable (initialize to 0) and increment it in the

if ~isempty

statement, and use that variable in legendms,

or

use legendms{end+1} as @notlikethat said to stretch it out, after initializing it to an empty cell.

PS: if you use hold all instead of hold on, you do not need to define the line colors.

OTHER TIPS

It looks to me like each legend entry is the accumulation of all the previous entries, which does not seem correct to me. I think I would replace this line of code:

legendms{m} = [legendms ' ' files(i,1).sta ' ' files(i,1).loc ' ' files(i,1).comp ' '  

monthString{m} ' ' yString{y}(3:4)];

with this line of code:

legendms{m} = [' ' files(i,1).sta ' ' files(i,1).loc ' ' files(i,1).comp ' '  

monthString{m} ' ' yString{y}(3:4)];

Since you are missing some data, you will want to remove the blank entries from the legend cell array before calling legend. I suggest adding code like this to remove the empty cell array elements.

is_empty = cellfun(@isempty, legendms);
legendms = legendms(~is_empty);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top