-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhline_new.m
140 lines (132 loc) · 3.94 KB
/
hline_new.m
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function hhh=hline_new(y,in1,in2,in3)
% function h=hline(y, linetype, linewidth, label)
%
% Draws a horizontal line on the current axes at the location specified by 'y'. Optional arguments are
% 'linetype' (default is 'r:'), linewidth, which specifies the width of the plotted line
% and 'label', which applies a text label to the graph near the line. The
% label appears in the same color as the line.
%
% The line is held on the current axes, and after plotting the line, the function returns the axes to
% its prior hold state.
%
% The HandleVisibility property of the line object is set to "off", so not only does it not appear on
% legends, but it is not findable by using findobj. Specifying an output argument causes the function to
% return a handle to the line, so it can be manipulated or deleted. Also, the HandleVisibility can be
% overridden by setting the root's ShowHiddenHandles property to on.
%
% h = hline(42,'g','The Answer')
%
% returns a handle to a green horizontal line on the current axes at y=42, and creates a text object on
% the current axes, close to the line, which reads "The Answer".
%
% hline also supports vector inputs to draw multiple lines at once. For example,
%
% hline([4 8 12],{'g','r','b'},{'l1','lab2','LABELC'})
%
% draws three lines with the appropriate labels and colors.
%
% By Brandon Kuczenski for Kensington Labs.
% 8 November 2001
% Modified Timothy Laumann 03/02/11
if length(y)>1 % vector input
for I=1:length(y)
switch nargin
case 1
linetype='r:';
label='';
case 2
if ~iscell(in1)
in1={in1};
end
if I>length(in1)
linetype=in1{end};
else
linetype=in1{I};
end
label='';
case 3
if ~iscell(in1)
in1={in1};
end
if ~iscell(in2)
in2={in2};
end
if I>length(in1)
linetype=in1{end};
else
linetype=in1{I};
end
if I>length(in2)
linewidth=in2{end};
else
linewidth=in2{I};
end
label='';
case 4
if ~iscell(in1)
in1={in1};
end
if ~iscell(in2)
in2={in2};
end
if ~iscell(in3)
in3={in3};
end
if I>length(in1)
linetype=in1{end};
else
linetype=in1{I};
end
if I>length(in2)
linewidth=in2{end};
else
linewidth=in2{I};
end
if I>length(in3)
label=in3{end};
else
label=in3{I};
end
end
h(I)=hline_new(y(I),linetype, linewidth, label);
end
else
switch nargin
case 1
linetype='r:';
label='';
case 2
linetype=in1;
label='';
case 3
linetype=in1;
linewidth=in2;
label='';
case 4
linetype=in1;
linewidth=in2;
label=in3;
end
g=ishold(gca);
hold on
x=get(gca,'xlim');
h=plot(x,[y y],linetype,'LineWidth',linewidth);
if ~isempty(label)
yy=get(gca,'ylim');
yrange=yy(2)-yy(1);
yunit=(y-yy(1))/yrange;
if yunit<0.2
text(x(1)+0.02*(x(2)-x(1)),y+0.02*yrange,label,'color',get(h,'color'))
else
text(x(1)+0.02*(x(2)-x(1)),y-0.02*yrange,label,'color',get(h,'color'))
end
end
if g==0
hold off
end
set(h,'tag','hline','handlevisibility','off') % this last part is so that it doesn't show up on legends
end % else
if nargout
hhh=h;
end