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

X = [];
Y = [];
M = [];
SD = [];
SE = [];
SDM = [];
SEM = [];
bin = 0;
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; i];
      M = [M; nanmedian1(y(j))];
      Y = [Y; nanmean1(y(j))];
      SD = [SD; std(y(j),1)];
      SE = [SE; std(y(j),1)/sqrt(length(j))];
      SDM = [SDM; sqrt(nanmean1((y(j)-nanmedian1(y(j))).^2))];
      SEM = [SEM; sqrt(nanmean1((y(j)-nanmedian1(y(j))).^2))/sqrt(length(j))];
   else
      bin = bin + 1;
      X = [X; i];
      M = [M; NaN];
      Y = [Y; NaN];
      SD = [SD; NaN];
      SE = [SE; NaN];
      SDM = [SDM; NaN];
      SEM = [SEM; NaN];
   end
end

