-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpsLegend.m
More file actions
40 lines (38 loc) · 973 Bytes
/
cpsLegend.m
File metadata and controls
40 lines (38 loc) · 973 Bytes
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
function legendhandle=cpsLegend(ax)
% EXAMPLE:
% cpsFindFig('cpsAddLegend example');
% clf
% h=plot(rand(10,1));
% h.Tag='legend:Line#1';
% hold on
% h=plot(rand(10,1));
% h.Tag='legend:Line#2';
% cpsLegend
if nargin==0
ax=gca;
end
% remove existing legends if there are any
ch=get(ax,'Children');
for i=1:numel(ch)
if isa(ch(i),'matlab.graphics.illustration.Legend')
delete(ch(i));
%ch(i)=[];
end
end
h=[];
lbl={};
for i=1:numel(ch)
start=strfind(lower(ch(i).Tag),'<lgnd>')+numel('<lgnd>');
if ~isempty(start)
stop=strfind(lower(ch(i).Tag),'</lgnd>');
if isempty(stop)
stop=numel(ch(i).Tag);
end
h(end+1)=ch(i); %#ok<AGROW>
lbl{end+1}=ch(i).Tag(start:stop-1); %#ok<AGROW>
end
end
if ~isempty(h)
legendhandle=legend(ax,h,lbl);
end
end