Skip entries in a legend when plotting from a table
    조회 수: 7 (최근 30일)
  
       이전 댓글 표시
    
I have got a table from which I'd like to plot two fields ('Analysis' against 'Date'). The 'Analysis' field is a table itself, and I am plotting a particular field from it (PMkW_corr_298_RPMcorr). The health_monitor_log table has a field, 'Unit', from which I'd like to include unique values of in the legend. Each unique Unit has a different marker and/or colour.
I have tried the code below, but it doesn't seem to exclude repeat values of Unit from the legend. So for my first unique Unit entry, I'm expecting a triangle marker in the legend, and for the second unique Unit, a diamond. But when I type two entries for the legend in, I get two triangles, which is because the first two entries in the table have the triangle markers.
                          [c,ia,ic]=unique(health_monitor_log.Unit);
                          marker={'^','d','<','>','s','p','h'};
                          colour={'k','r','y','g','b','c','m'};
                          for i=1:size(health_monitor_log,1)
                              hold on
                              if any(strcmp('etath_PM_kW',fieldnames(health_monitor_log.Analysis{i,1})))                           
                                  p=plot(health_monitor_log.Date(i),health_monitor_log.Analysis{i}.PMkW_corr_298_RPMcorr,'Marker',char(marker(health_monitor_log.Marker(i))),'MarkerFaceColor',char(colour(health_monitor_log.Colour(i))),'MarkerEdgeColor',[1,1,1])
                                  if ~ismember(i,ia)
                                      ant=get(p,'Annotation');
                                      if iscell(ant)
                                          for j=1:length(ant)
                                              set(get(ant{j},'LegendInformation'),'IconDisplayStyle','off')
                                          end
                                      else
                                          set(get(ant,'LegendInformation'),'IconDisplayStyle','off')
                                      end
                                  end
                              end
                          end
댓글 수: 2
  Peter Perkins
    
 2018년 1월 24일
				This
   health_monitor_log.Analysis{i}.PMkW_corr_298_RPMcorr
suggests that Analysis is not a table, but rather a cell array of tables. Nothing at all wrong with that, it would be a collection of data sets, each one tagged by a date, I guess? But then you are plotting it against Date(i) which seems like it would be ONE date.
It would help to see a simple example of what data you have.
채택된 답변
추가 답변 (1개)
  Rik
      
      
 2018년 1월 24일
        Use explicit handles in your call to legend. Make sure you only pass 1 element of each type into the handle array.
h_d=plot(1,2,'d');
hold on
h_t=plot([.5 .7;.6 .8],[1.5 1.5;1 1],'*');
legend([h_d(1) h_t(1)],{'diamond','triangle'})
댓글 수: 3
  Rik
      
      
 2018년 1월 24일
				That p in your code is a handle to a line object. You can use that to populate an array of handles. What is the problem precisely? I don't have your data, so I can't really reproduce what you want. Can you provide some dummy data that looks like the data you have?
참고 항목
카테고리
				Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!