function [X, Y, M, SD, SE, SDM, SEM, nm,sig] = binave_lb2(x,y,range)

% N=(xmax-xmin)/binsize;
N=length(range);
X = zeros(N,1);
Y = X;
M = X;
SD = X;
SE = X;
SDM = X;
SEM = X;
sig=X;
bin = 0;
nm=X;
for i = 1:N-1
    j = find(x>range(i) & x<range(i+1) & ~isnan(x) & ~isnan(y) & isfinite(x) & isfinite(y));
    bin = bin + 1;
    if ~isempty(j)
        X(bin) = nanmean(x(j));
        M(bin)=nanmedian(y(j));
        Y(bin)=nanmean(y(j));
        SD(bin)=nanstd(y(j),1);
        SE(bin)=nanstd(y(j),1)/sqrt(length(j));
        SDM(bin)=sqrt(nanmean((y(j)-nanmedian(y(j))).^2));
        SEM(bin)=sqrt(nanmean((y(j)-nanmedian(y(j))).^2))/sqrt(length(j));
        nm(bin)=length(j);
        
        flxxu=sortrows(y(j));nxu=length(flxxu);
        jlft=floor(max(.15*nxu,1));jrgt=floor(max(1,.85*nxu));
        sig(bin)=(flxxu(jrgt)-flxxu(jlft))/2;
    end
end

