function r2 = computeR2Value(datahandle,fitnum,normalized,pp) 

% This function compute r2 value for a particular fit given if it is
% normalized. For spline and shape-preserving, we do not display numeric
% results.
% Copyright 2019-2021 The MathWorks, Inc.

ydata = double(get(datahandle,'YData'));
xdata = double(get(datahandle,'XData'));

if normalized
    normalized = getappdata(datahandle,'Basic_Fit_Normalizers');
    meanx = normalized(1);
    stdx = normalized(2);
    xdata  = (xdata - meanx)./(stdx);
end

% for spline and shape-preserving
if fitnum < 2
    y_fit = ppval(pp,xdata);
else
    % rest of the fits
    y_fit = polyval(pp,xdata);
end

r = corrcoef(ydata,y_fit);
r2 = r(1,2).^2;