function plotegfit(data, params, nbins)
% Plots an histogram of data and overlay ex-Gaussian PDF with parameters params = [mu sigma tau]
% data must be a vector
% nbins is optional and allows explicitly setting the number of bins
% (c) Yves Lacouture, Universite Laval
% Version 2.0 27/02/03
[n,m]=size(data);
if min(n,m) > 1
   error('First argument must be a vector');
end
if  n == 1        	                   %case of a row vector of data
	data = data';
	n = m;
end
if nargin < 2
   error('Second argument must be parameter values [mu sigma tau]');
end

if nargin < 3
   nbins = ceil(sqrt(n));     % default number of bins
end

x=(min(data):0.1:max(data))'; % evenly spaced x values for plot
[nb, xb]=hist(data,nbins);    % compute bins values
hand=bar(xb,nb,1,'w');        % plot histogram, no gaps, fill in white
set(gca,'NextPlot','add');

xd=get(hand,'Xdata');         % get x values for the histogram
rangex=max(xd(:)) - min(xd(:));
bw=rangex/nbins;              % compute bin width

egp=exgausspdf(params(1),params(2),params(3),x); % gets ex-Gaussian density
egp=n*(egp*bw);									 % normalize egp
plot(x,egp,'k-');
