0001 function add_errorbars(h,errordata,linecolor,orientation)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 ncond = length(h);
0018
0019 if nargin < 3
0020 linecolor = '';
0021 end
0022
0023 if nargin < 4
0024 orientation = 'vertical';
0025 if strcmp(get(h(1),'Horizontal'),'on')
0026 if verLessThan('matlab','8.4.0')
0027 orientation = 'horizontal';
0028 else
0029 orientation = 'new_horizontal';
0030 end
0031 end
0032 end
0033
0034 groupwidth = 0.8;
0035
0036 obj_type = get(h(1),'Type');
0037 switch obj_type
0038 case {'patch','bar'}
0039 case 'hggroup'
0040
0041
0042
0043 new_kids = get(h,'Children');
0044 if ~iscell(new_kids)
0045 h = new_kids;
0046 else
0047 h = [new_kids{:}];
0048 end
0049
0050 otherwise
0051 warning('\nadd_errorbars: Unsupported object type: %s\n', obj_type)
0052 return
0053 end
0054
0055 if size(errordata,2) ~= ncond
0056 error('Number of conditions (columns) does not match: expected %d, found %d', ncond,size(errordata,2))
0057 end
0058
0059 if size(get(h(1),'XData'),2) ~= size(errordata,1)
0060 error('Number of rows does not match: expected %d, found %d', size(get(h(1),'XData'),2), size(errordata,1))
0061 end
0062
0063 figure(gcf)
0064 axes(gca)
0065 hold on
0066
0067
0068 groupwidth = min(groupwidth, ncond/(ncond+1.5));
0069 widthPerCond = groupwidth/ncond;
0070
0071 for icond = 1:ncond
0072 xdata = get(h(icond),'XData');
0073 ydata = get(h(icond),'YData');
0074
0075 nbar = size(xdata,2);
0076
0077 condOffset = (icond-mean(1:ncond))*widthPerCond;
0078
0079 for ibar = 1:nbar
0080 switch orientation
0081 case 'vertical'
0082 ylength = errordata(ibar,icond);
0083
0084 if strcmp(obj_type,'bar')
0085 xoffset = xdata(ibar)+condOffset;
0086 ystart = ydata(ibar);
0087 cap_length = widthPerCond*get(h(icond),'BarWidth')/2;
0088
0089 else
0090 currXData = xdata([2 3], ibar);
0091 cap_length = diff(currXData)/2;
0092
0093
0094 xoffset = mean(currXData);
0095 ystart = ydata(2,ibar);
0096 end
0097
0098 if ystart >= 0
0099 ystop = ystart + ylength;
0100 else
0101 ystop = ystart - ylength;
0102 end
0103 if ~isempty(linecolor)
0104 line([xoffset xoffset], [ystart ystop],'color',linecolor);
0105 else
0106 line([xoffset xoffset], [ystart ystop]);
0107 end
0108
0109
0110 xstart = xoffset-cap_length/2;
0111 xstop = xstart + cap_length;
0112 if ~isempty(linecolor)
0113 line([xstart xstop], [ystop ystop],'color',linecolor);
0114 else
0115 line([xstart xstop], [ystop ystop]);
0116 end
0117 case 'horizontal'
0118 xlength = errordata(ibar,icond);
0119 cap_length = diff(ydata([2 3], ibar))/2;
0120
0121
0122 yoffset = mean(ydata([2 3],ibar));
0123 xstart = xdata(2,ibar);
0124 if xstart >= 0
0125 xstop = xstart + xlength;
0126 else
0127 xstop = xstart - xlength;
0128 end
0129 if ~isempty(linecolor)
0130 line([xstart xstop], [yoffset yoffset],'color',linecolor);
0131 else
0132 line([xstart xstop], [yoffset yoffset]);
0133 end
0134
0135
0136 ystart = yoffset-cap_length/2;
0137 ystop = ystart + cap_length;
0138 if ~isempty(linecolor)
0139 line([xstop xstop], [ystart ystop],'color',linecolor);
0140 else
0141 line([xstop xstop], [ystart ystop]);
0142 end
0143 case 'new_horizontal'
0144 xlength = errordata(ibar,icond);
0145 xstart = ydata(ibar);
0146 xstop = xstart + xlength;
0147
0148
0149 yoffset = xdata(ibar);
0150 if ~isempty(linecolor)
0151 line([xstart xstop], [yoffset yoffset],'color',linecolor);
0152 else
0153 line([xstart xstop], [yoffset yoffset]);
0154 end
0155
0156
0157 cap_length = widthPerCond*get(h(icond),'BarWidth')/2;
0158 ystart = yoffset-cap_length/2;
0159 ystop = ystart + cap_length;
0160 if ~isempty(linecolor)
0161 line([xstop xstop], [ystart ystop],'color',linecolor);
0162 else
0163 line([xstop xstop], [ystart ystop]);
0164 end
0165 end
0166 end
0167 end
0168
0169 hold off