-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpsStructLegend.m
More file actions
62 lines (56 loc) · 1.55 KB
/
cpsStructLegend.m
File metadata and controls
62 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function [LEGH,OBJH,OUTH,OUTM] = cpsStructLegend(ax,S,spaceCode)
% EXAMPLE 1:
% h.House=plot(rand(10,1));
% hold on;
% h.Face=plot(rand(10,1));
% lh=cpsStructLegend(gca,h)
% lh.FontSize=12;
%
% EXAMPLE 2:
% h.my_house=plot(rand(10,1));
% hold on;
% h.my_face=plot(rand(10,1));
% lh=cpsStructLegend(h,'_')
% lh.FontSize=12;
% handle the optionality of the first argument
if nargin==1
S=ax;
ax=gca;
spaceCode='';
elseif nargin==2
if ~isa(ax,'matlab.graphics.axis.Axes')
spaceCode=S;
S=ax;
ax=gca;
else
spaceCode='';
end
elseif nargin==3
if isempty(spaceCode)
spaceCode=''; % make sure it's empty of the char kind
end
end
% check the inputs
if ~isa(ax,'matlab.graphics.axis.Axes')
error('ax should be a matlab.graphics.axis.Axes');
end
if ~isstruct(S)
error('S should be struct');
end
if ~ischar(spaceCode)
error('spaceCode should be char');
end
labels=fieldnames(S);
handles=nan(size(labels));
for i=1:numel(labels)
if ~isempty(S.(labels{i}))
handles(i)=S.(labels{i});
end
% replace space code with whitespace. if spaceCode=='' nothing happens
labels(i)=strrep(labels(i),spaceCode,' ');
end
notthere=isnan(handles);
handles(notthere)=[];
labels(notthere)=[];
[LEGH,OBJH,OUTH,OUTM] = legend(ax,handles,labels);
end