function [errorPatch,dataLine] = niceBars(varargin)
% NICEBARS plots transparent patch errorbars around a given curve
%
%     [errorPatch,dataLine,t] = NICEBARS(X, Y, LCI, UCI, Color, Alpha)
%     Plots a transperent error patches around a line. Upper and lower
%     error bars are independent. Color and alpha are optional arguments.
%
%     USAGE :
%              1)   [errorPatch,dataLine] = niceBars(x,y,CI)
%                    Calculates the Lower and upper errorbars as
%                    L = Y-E and U = Y+E. It then takes a default color
%                    as patch color, and the line color as black and plots
%                    it around the line using a patch object.
%
%              2)   [errorPatch,dataLine] = niceBars(x,y,LCI, UCI)
%                    Calculates the Lower and upper errorbars as
%                    L = Y-E and U = Y+E.
%
%              3)   [errorPatch,dataLine] = niceBars(x,y,CI,color,alpha)
%                    User single error CI, It then takes a custom color and
%                    alpha value.
%
%              4)   [errorPatch,dataLine] = niceBars(x,y,LCI,UCI,color,alpha)
%                    User Supplied bounds are taken as LCI and UCI, It then
%                   takes a custom color and alpha value.

%      EXAMPLE
%                         t = [-5:0.05:5];
%                         Y = sin(t);
%                         E = 0.4*rand(1,length(t));
%                         [errorPatch,dataLine] = niceBars(t,Y,E);
%                         xlabel('time');
%                         ylabel('Amplitude');
%                         title('Using Errors alone');
%
% Version 1.0 Bob Colner April Fools Day 2009

switch(nargin)
    case 3,
        % If there are 3 inputs it means its just the errors
        x = varargin{1};
        if size(x,1)>1
            x = x';
        end
        y = varargin{2};
        if size(y,1)>1
            y = y';
        end
        E = varargin{3};
        if size(E,1)>1
            E = E';
        end
        L = y - E;
        U = y + E;
        color = [0 0 0]; alpha = 0.5; linest = 'none';
    case 4,
        % If there are 4 inputs it means they entered the Lower/upper bounds
        x = varargin{1}; 
        if size(x,1)>1
            x = x';
        end
        y = varargin{2};
        if size(y,1)>1
            y = y';
        end
        L = varargin{3};
        if size(L,1)>1
            L = L';
        end
        U = varargin{4};
        if size(U,1)>1
            U = U';
        end
        color = [0 0 0]; alpha = 0.5;linest = 'none';
    case 5%6,
        % equal error bars and custom color and alpha and linestyle
        x = varargin{1};
        if size(x,1)>1
            x = x';
        end
        y = varargin{2};
        if size(y,1)>1
            y = y';
        end
        E = varargin{3};
        if size(E,1)>1
            E = E';
        end
        L = y - E;
        U = y + E;
        color =  varargin{4}; alpha = varargin{5}; %linest = varargin{6};
    case 6%7,
        % independent error bars and custom color and alpha and linestyle
        x = varargin{1};
        if size(x,1)>1
            x = x';
        end
        y = varargin{2};
        if size(y,1)>1
            y = y';
        end
        L = varargin{3};
        if size(L,1)>1
            L = L';
        end
        U = varargin{4};
        if size(U,1)>1
            U = U';
        end
        color =  varargin{5}; alpha = varargin{6}; %linest = varargin{7};
end
dataLine = plot(x,y,'color',color,'linewidth',4,'linestyle','-');%linest);% plot data line
hold on;
Xcoords = [x x(end:-1:1)];% X-coords for CI patch 
Ycoords = [U L(end:-1:1)];% Y-coords for CI patch 
errorPatch = patch(Xcoords,Ycoords,color);% draw error bar patch
set(errorPatch,'linestyle','none','linewidth',2,'faceAlpha',alpha);% set 'errorPatch' style

set(get(get(errorPatch,'Annotation'),'LegendInformation'),'IconDisplayStyle','off'); % Exclude patch from legend
% set(get(errorPatch,'LegendInformation'),'IconDisplayStyle','off'); % Exclude patch from legend

% set(gcf, 'color', 'white')% set figure backgroup to white
% set(gca, 'fontsize', 14)% set axis font size
hold off;