| 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: [email protected] |
| 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/[email protected]^[email protected] |
| 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 ~ischar(l), |
| 48 |
l = l(:); |
| 49 |
end |
| 50 |
if nargin > 3 |
| 51 |
if ~ischar(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 ~ischar(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 ischar(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 ischar(x) || ischar(y) || ischar(u) || ischar(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 |
tee = (max(y(:))-min(y(:)))/100; % make tee .02 x-distance for error bars |
| 104 |
% changed from errorbar.m |
| 105 |
xl = x - l; |
| 106 |
xr = x + u; |
| 107 |
ytop = y + tee; |
| 108 |
ybot = y - tee; |
| 109 |
n = size(y,2); |
| 110 |
% end change |
| 111 |
|
| 112 |
% Plot graph and bars |
| 113 |
hold_state = ishold; |
| 114 |
cax = newplot; |
| 115 |
next = lower(get(cax,'NextPlot')); |
| 116 |
|
| 117 |
% build up nan-separated vector for bars |
| 118 |
% changed from errorbar.m |
| 119 |
xb = zeros(npt*9,n); |
| 120 |
xb(1:9:end,:) = xl; |
| 121 |
xb(2:9:end,:) = xl; |
| 122 |
xb(3:9:end,:) = NaN; |
| 123 |
xb(4:9:end,:) = xl; |
| 124 |
xb(5:9:end,:) = xr; |
| 125 |
xb(6:9:end,:) = NaN; |
| 126 |
xb(7:9:end,:) = xr; |
| 127 |
xb(8:9:end,:) = xr; |
| 128 |
xb(9:9:end,:) = NaN; |
| 129 |
|
| 130 |
yb = zeros(npt*9,n); |
| 131 |
yb(1:9:end,:) = ytop; |
| 132 |
yb(2:9:end,:) = ybot; |
| 133 |
yb(3:9:end,:) = NaN; |
| 134 |
yb(4:9:end,:) = y; |
| 135 |
yb(5:9:end,:) = y; |
| 136 |
yb(6:9:end,:) = NaN; |
| 137 |
yb(7:9:end,:) = ytop; |
| 138 |
yb(8:9:end,:) = ybot; |
| 139 |
yb(9:9:end,:) = NaN; |
| 140 |
% end change |
| 141 |
|
| 142 |
|
| 143 |
[ls,col,mark,msg] = colstyle(symbol); |
| 144 |
if ~isempty(msg) |
| 145 |
error(msg); |
| 146 |
end |
| 147 |
if isempty(col) |
| 148 |
col = ''; |
| 149 |
end |
| 150 |
symbol = [ls mark col]; % Use marker only on data part |
| 151 |
esymbol = ['-' col]; % Make sure bars are solid |
| 152 |
if ~isempty(strfind(symbol,'none')) |
| 153 |
symbol = 'none'; |
| 154 |
end |
| 155 |
if ~isempty(strfind(esymbol,'none')) |
| 156 |
esymbol = 'none'; |
| 157 |
end |
| 158 |
h = plot(xb,yb,'LineStyle',esymbol); hold on |
| 159 |
h = [h;plot(x,y,'LineStyle',symbol)]; |
| 160 |
|
| 161 |
if ~hold_state |
| 162 |
hold off; |
| 163 |
end |
| 164 |
|
| 165 |
if nargout>0 |
| 166 |
hh = h; |
| 167 |
end |