1 | function hh = herrorbar(x, y, l, u, symbol) |
---|
2 | %HERRORBAR Horizontal Error bar plot. |
---|
3 | % HERRORBAR(X,Y,L,R) plots the graph of vector X vs. vector Y with |
---|
4 | % horizontal error bars specified by the vectors L and R. L and R contain the |
---|
5 | % left and right error ranges for each point in X. Each error bar |
---|
6 | % is L(i) + R(i) long and is drawn a distance of L(i) to the right and R(i) |
---|
7 | % to the right the points in (X,Y). The vectors X,Y,L and R must all be |
---|
8 | % the same length. If X,Y,L and R are matrices then each column |
---|
9 | % produces a separate line. |
---|
10 | % |
---|
11 | % HERRORBAR(X,Y,E) or HERRORBAR(Y,E) plots X with error bars [X-E X+E]. |
---|
12 | % HERRORBAR(...,'LineSpec') uses the color and linestyle specified by |
---|
13 | % the string 'LineSpec'. See PLOT for possibilities. |
---|
14 | % |
---|
15 | % H = HERRORBAR(...) returns a vector of line handles. |
---|
16 | % |
---|
17 | % Example: |
---|
18 | % x = 1:10; |
---|
19 | % y = sin(x); |
---|
20 | % e = std(y)*ones(size(x)); |
---|
21 | % herrorbar(x,y,e) |
---|
22 | % draws symmetric horizontal error bars of unit standard deviation. |
---|
23 | % |
---|
24 | % This code is based on ERRORBAR provided in MATLAB. |
---|
25 | % |
---|
26 | % See also ERRORBAR |
---|
27 | |
---|
28 | % Jos van der Geest |
---|
29 | % email: jos@jasen.nl |
---|
30 | % |
---|
31 | % File history: |
---|
32 | % August 2006 (Jos): I have taken back ownership. I like to thank Greg Aloe from |
---|
33 | % The MathWorks who originally introduced this piece of code to the |
---|
34 | % Matlab File Exchange. |
---|
35 | % September 2003 (Greg Aloe): This code was originally provided by Jos |
---|
36 | % from the newsgroup comp.soft-sys.matlab: |
---|
37 | % http://newsreader.mathworks.com/WebX?50@118.fdnxaJz9btF^1@.eea3ff9 |
---|
38 | % After unsuccessfully attempting to contact the orignal author, I |
---|
39 | % decided to take ownership so that others could benefit from finding it |
---|
40 | % on the MATLAB Central File Exchange. |
---|
41 | |
---|
42 | if min(size(x))==1, |
---|
43 | npt = length(x); |
---|
44 | x = x(:); |
---|
45 | y = y(:); |
---|
46 | if nargin > 2, |
---|
47 | if ~isstr(l), |
---|
48 | l = l(:); |
---|
49 | end |
---|
50 | if nargin > 3 |
---|
51 | if ~isstr(u) |
---|
52 | u = u(:); |
---|
53 | end |
---|
54 | end |
---|
55 | end |
---|
56 | else |
---|
57 | [npt,n] = size(x); |
---|
58 | end |
---|
59 | |
---|
60 | if nargin == 3 |
---|
61 | if ~isstr(l) |
---|
62 | u = l; |
---|
63 | symbol = '-'; |
---|
64 | else |
---|
65 | symbol = l; |
---|
66 | l = y; |
---|
67 | u = y; |
---|
68 | y = x; |
---|
69 | [m,n] = size(y); |
---|
70 | x(:) = (1:npt)'*ones(1,n);; |
---|
71 | end |
---|
72 | end |
---|
73 | |
---|
74 | if nargin == 4 |
---|
75 | if isstr(u), |
---|
76 | symbol = u; |
---|
77 | u = l; |
---|
78 | else |
---|
79 | symbol = '-'; |
---|
80 | end |
---|
81 | end |
---|
82 | |
---|
83 | if nargin == 2 |
---|
84 | l = y; |
---|
85 | u = y; |
---|
86 | y = x; |
---|
87 | [m,n] = size(y); |
---|
88 | x(:) = (1:npt)'*ones(1,n);; |
---|
89 | symbol = '-'; |
---|
90 | end |
---|
91 | |
---|
92 | u = abs(u); |
---|
93 | l = abs(l); |
---|
94 | |
---|
95 | if isstr(x) | isstr(y) | isstr(u) | isstr(l) |
---|
96 | error('Arguments must be numeric.') |
---|
97 | end |
---|
98 | |
---|
99 | if ~isequal(size(x),size(y)) | ~isequal(size(x),size(l)) | ~isequal(size(x),size(u)), |
---|
100 | error('The sizes of X, Y, L and U must be the same.'); |
---|
101 | end |
---|
102 | |
---|
103 | if strcmp(get(gca,'ylimmode'),'manual') |
---|
104 | my_ylim = ylim; |
---|
105 | index = find(y >= my_ylim(1) & y <= my_ylim(2)); |
---|
106 | tee = (max(y(index)) - min(y(index)))/100; |
---|
107 | else |
---|
108 | tee = (max(y(:))-min(y(:)))/100 % make tee .02 x-distance for error bars |
---|
109 | end |
---|
110 | % changed from errorbar.m |
---|
111 | xl = x - l; |
---|
112 | xr = x + u; |
---|
113 | ytop = y + tee; |
---|
114 | ybot = y - tee; |
---|
115 | n = size(y,2); |
---|
116 | % end change |
---|
117 | |
---|
118 | % Plot graph and bars |
---|
119 | hold_state = ishold; |
---|
120 | cax = newplot; |
---|
121 | next = lower(get(cax,'NextPlot')); |
---|
122 | |
---|
123 | % build up nan-separated vector for bars |
---|
124 | % changed from errorbar.m |
---|
125 | xb = zeros(npt*9,n); |
---|
126 | xb(1:9:end,:) = xl; |
---|
127 | xb(2:9:end,:) = xl; |
---|
128 | xb(3:9:end,:) = NaN; |
---|
129 | xb(4:9:end,:) = xl; |
---|
130 | xb(5:9:end,:) = xr; |
---|
131 | xb(6:9:end,:) = NaN; |
---|
132 | xb(7:9:end,:) = xr; |
---|
133 | xb(8:9:end,:) = xr; |
---|
134 | xb(9:9:end,:) = NaN; |
---|
135 | |
---|
136 | yb = zeros(npt*9,n); |
---|
137 | yb(1:9:end,:) = ytop; |
---|
138 | yb(2:9:end,:) = ybot; |
---|
139 | yb(3:9:end,:) = NaN; |
---|
140 | yb(4:9:end,:) = y; |
---|
141 | yb(5:9:end,:) = y; |
---|
142 | yb(6:9:end,:) = NaN; |
---|
143 | yb(7:9:end,:) = ytop; |
---|
144 | yb(8:9:end,:) = ybot; |
---|
145 | yb(9:9:end,:) = NaN; |
---|
146 | % end change |
---|
147 | |
---|
148 | |
---|
149 | [ls,col,mark,msg] = colstyle(symbol); if ~isempty(msg), error(msg); end |
---|
150 | symbol = [ls mark col]; % Use marker only on data part |
---|
151 | esymbol = ['-' col]; % Make sure bars are solid |
---|
152 | |
---|
153 | h = plot(xb,yb,esymbol); hold on |
---|
154 | h = [h;plot(x,y,symbol)]; |
---|
155 | |
---|
156 | if ~hold_state, hold off; end |
---|
157 | |
---|
158 | if nargout>0, hh = h; end |
---|