Skip empty array in plot legend?

6 Ansichten (letzte 30 Tage)
David Stolnis
David Stolnis am 12 Apr. 2019
Bearbeitet: Adam Danz am 12 Apr. 2019
I'm using logical arrays to plot various levels of threshold excedance.
How can I skip legend titles of empty arrays? In the following screenshots you'll see all levels of threholds exceeded, next you will see where only low level of thresholds are exceeded. and the legend is incorrect. As a note on the second figure, the yellow dots shold be "exceeds construction lims".
The bottom plots legend should have "exceeds constuction lims" instead of "Level1"
I'm assingin variable names to each plot and using hold on. Simplified example code:
a = plot(the blue line);
b = plot (logical array of level 1);
c = plot(logical array of level 2);
d = plot(logical array of exceeds construction lims);
legend([a b c d], {'blue line', level1', 'level2', 'exceed const lims'});
I need to be able top skip level1 and level2 if they are 0x1 empty arrays.
I cannot simply change the order because for somethings I do not have constuction limits so I would run into the ame issue by just reordering my plots.

Antworten (2)

Matt J
Matt J am 12 Apr. 2019
Bearbeitet: Matt J am 12 Apr. 2019
Why not something like this?
allLines=[a,b,c,d];
allStrings={'blue line', 'level1', 'level2', 'exceed const lims'};
legend(allLines(subset), allStrings(subset))
where subset is a logical array based on your inclusion criteria for the legend.

Adam Danz
Adam Danz am 12 Apr. 2019
Bearbeitet: Adam Danz am 12 Apr. 2019
Try this.
% List all possible legend handles and their strings
legHands = {a b c d};
legStrs = {'blue line', level1', 'level2', 'exceed const lims'};
% Determine which handles aren't empty
hasData = ~cellfun(@isempty, legHands);
% Produce legend, ignore empties
legend([legHands{hasData}], legStrs{hasData})

Produkte


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by