How to skip the legends for some series of data that's actually Null after some Calculation?

Hello, everyone. Here is the problem I am facing. Say I have 3 series of data, but some of the series could be of length 0. As I plotted them all on the same figure, I want to legend them correctly, how should I do that more compactly? Below is what I got ( notice the data are all in the structure Performance)
if cellfun('isempty', Performance.Apple)
legend('Orange', 'Banana');
elseif cellfun('isempty', Performance.Orange)
legend('Banana', 'Orange');
elseif cellfun('isempty', Performance.Banana)
legend('Apple', 'Orange');
elseif cellfun('isempty', Performance.Apple) & cellfun('isempty', Performance.Orange)
legend('Banana');
elseif cellfun('isempty', Performance.Apple) & cellfun('isempty', Performance.Banana)
legend('Orange');
elseif cellfun('isempty', Performance.Orange) & cellfun('isempty', Performance.Banana)
legend('Apple');
end
So you get what I'm trying to do, right? I know there must be a trick to do these smartly, please help me.

Answers (1)

LegendVector = {'Apple', 'Orange', 'Banana'};
ActualLegend = [~cellfun('isempty', Performance.Apple),...
~cellfun('isempty', Performance.Orange), ...
~cellfun('isempty', Performance.Banana)];
LegendVector = LegendVector(ActualLegend);
legend(LegendVector);
This is by far what came to my mind, is there any other way to simplify the redundent cellfun in my code?

Categories

Asked:

on 24 Sep 2015

Edited:

on 24 Sep 2015

Community Treasure Hunt

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

Start Hunting!