Home > utils > add_errorbars.m

add_errorbars

PURPOSE ^

SYNOPSIS ^

function add_errorbars(h,errordata,linecolor,orientation)

DESCRIPTION ^

 add_errorbars(h,errordata);

 Adds error bars to bar plots.  The bar information is in h which is a vector
 of object handles returned by bar().  Error data is the error data to be
 plotted.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function add_errorbars(h,errordata,linecolor,orientation)
0002 %
0003 % add_errorbars(h,errordata);
0004 %
0005 % Adds error bars to bar plots.  The bar information is in h which is a vector
0006 % of object handles returned by bar().  Error data is the error data to be
0007 % plotted.
0008 
0009 % 02/09/05 PJ -- Added a bit more dimension error checking, and object type
0010 % checking
0011 % 11/03/06 PJ -- Added handling of Matlab 7 barseries (hggroup) objects
0012 % 11/11/06 PJ -- Fixed handling of single barseries object
0013 % 05/06/07 PJ -- Added support for horizontal bargraphs
0014 % 03/09/11 PJ -- added auto-dection of bar orientation
0015 % 22Jul2015 PJ -- made compatible with MATLAB version >= 2014b
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     % The patch object is actually buried deeper down. It is a child of the
0041     % hggroup object
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 % based on makebars.m
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           % Draw the vertical section
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         % Add the cap
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         % Draw the horizontal section
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         % Add the cap
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         % Draw the errorbar
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         % Draw the cap
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 % switch
0166   end % for ibar=
0167 end % for icond=
0168 
0169 hold off

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003