1

我正在尝试绘制一些跨越 2 年的月度统计数据,其中 1 年只有一个月的数据,而另一年有 11 个月的数据。我遇到麻烦的地方是图例标签,当我填写标签时,它需要第一年的一个月数据,并放在下一年的标签后面,就好像它正在填写那个月一样。这就是我的意思:

    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']

其中 13 是 2013 年的 2 位数字表示,12 是 2012 年的数字表示。即使我按顺序循环这些年,它也会这样做。我相信这是因为 2012 年的 Legendms 基本上是空的单元格,除了 12 月,所以它只是填补了这个空间。它也没有遵循适当的线条样式。当 12 月 12 日应该是不同的线型时,它将 1 月 13 日标记为与其他线型不同的线型“-”。有谁知道解决这个问题的方法,或者如何在保留完整图例标签的同时忽略空单元格?

这是我到目前为止的代码,也许我只是犯了一个简单的错误?任何帮助将不胜感激!

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

重申一下,我希望图例忽略任何年份的空单元格,并使用适当的颜色和车站标签按年份顺序绘制图例标签。2012 年的值应该是实线,2013 年的值应该是虚线,无论颜色代表它们描述的月份(lineColors)。

4

2 回答 2

1

您正在使用变量“m”增加legendms,它循环遍历所有文件,即使它们是空的。但是,您仅在文件不为空时才使用 semilogx 进行绘图。

任何一个:

创建一个新变量(初始化为 0)并在

if ~isempty

声明,并在legendms中使用该变量,

或者

在将其初始化为空单元格后,使用 legendms{end+1} 作为@notlikethat 将其拉伸。

PS:如果你使用hold all而不是hold on,你不需要定义线条颜色。

于 2014-01-27T20:32:43.157 回答
0

在我看来,每个图例条目都是以前所有条目的累积,这对我来说似乎不正确。我想我会替换这行代码:

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

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

使用这行代码:

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

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

由于您缺少一些数据,因此您需要在调用图例之前从图例单元格数组中删除空白条目。我建议添加这样的代码来删除空单元格数组元素。

is_empty = cellfun(@isempty, legendms);
legendms = legendms(~is_empty);
于 2014-01-27T14:39:06.440 回答