function [x_str, y_str] = bfitcomputedatastats(datahandle)
% BFITCOMPUTEDATASTATS Compute stats to fill Data Stats GUI table.

%   Copyright 1984-2014 The MathWorks, Inc.

% get data
xdata = double(get(datahandle,'xdata'));
ydata = double(get(datahandle,'ydata'));

% compute stats
xy = [xdata(:) ydata(:)];
% compute along 1st dim explicitly so works for 1 data point
results = [ min(xy,[],1,'omitnan'); 
    max(xy,[],1,'omitnan'); 
    mean(xy,1,'omitnan');
    median(xy,1,'omitnan');
    mode(xy, 1);
    std(xy,0,1,'omitnan');
    max(xy,[],1,'omitnan') - min(xy,[],1,'omitnan');];

xstats = results(:,1);
ystats = results(:,2);

[x.min,x.max,x.mean,x.median,x.mode,x.std,x.range] = deal(xstats(1),xstats(2),xstats(3),xstats(4),xstats(5),xstats(6),xstats(7));
[y.min,y.max,y.mean,y.median,y.mode,y.std,y.range] = deal(ystats(1),ystats(2),ystats(3),ystats(4),ystats(5),ystats(6),ystats(7));

% place to store stats
setappdata(double(datahandle),'Data_Stats_X',x);
setappdata(double(datahandle),'Data_Stats_Y',y);


format = '%-12.4g';
x_str = cellstr(num2str(xstats,format));
y_str = cellstr(num2str(ystats,format));
