Skip entries in a legend when plotting from a table
4 views (last 30 days)
Show older comments
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 Comments
Peter Perkins
on 24 Jan 2018
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.
Accepted Answer
More Answers (1)
Rik
on 24 Jan 2018
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 Comments
Rik
on 24 Jan 2018
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?
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!