function [X, Y, M, SD, SE, SDM, SEM, nm] = binave_lb(x,y,binsize,xmin,xmax)

X = [];
Y = [];
M = [];
SD = [];
SE = [];
SDM = [];
SEM = [];
bin = 0;
nm=[];
for i = xmin:binsize:xmax-binsize
   j = find(x>i & x<i+binsize & ~isnan(x) & ~isnan(y));
   if ~isempty(j)
      bin = bin + 1;
      X = [X; nanmean(x(j))];
      M = [M; nanmedian(y(j))];
      Y = [Y; nanmean(y(j))];
      SD = [SD; nanstd(y(j))];
      SE = [SE; nanstd(y(j))/sqrt(length(j))];
      SDM = [SDM; sqrt(nanmean((y(j)-nanmedian(y(j))).^2))];
      SEM = [SEM; sqrt(nanmean((y(j)-nanmedian(y(j))).^2))/sqrt(length(j))];
      nm=[nm; length(j)]; 
   end
end

