function [axesCount,fitschecked,bfinfo,evalresultsstr,evalresultsx,evalresultsy,...
        currentfit,coeffresidstrings] = bfitselectnew(figHandle, newdataHandle)
% BFITSELECTNEW Update basic fit GUI and figure from current data to new data.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.9 $  $Date: 2000/09/15 14:15:49 $

% for new data, was it showing before?
guistate = getappdata(newdataHandle,'_Basic_Fit_Gui_State');
if isempty(guistate) % new data
    %setup appdata and compute stats: nothing plotted since new data
    [axesCount,fitschecked,bfinfo,evalresults,currentfit] = bfitsetup(figHandle, newdataHandle); % data stats computed
    evalresultsstr = '';
    evalresultsx = '';
    evalresultsy = '';
    coeffresidstrings = ' ';
    
else % was showing before: get fit info to return, and replot
    [axesCount,fitschecked,bfinfo,evalresultsstr,evalresultsx,evalresultsy,currentfit,coeffresidstrings] = ...
        bfitgetcurrentinfo(newdataHandle);
    
    strings = ' ';
    errmsg = '';
    
    axesH = get(newdataHandle,'parent'); % need this in case subplots in figure
    figH = get(axesH,'parent');
    infarray = repmat(inf,12,1);
    figure(figH)
    bfitlistenoff(figH)
        
    for fitindex = find(fitschecked)
        fit = fitindex - 1;
        [strings, err, pp, resid] = bfitcalcfit(newdataHandle,fit);
        if err
            error('Data was changed to have repeated values since last selection.')
        end
        if isequal(fit,currentfit) % so updates when normalized/unnormalized.
            coeffresidstrings = strings;
        end
        curveH = bfitplotfit(newdataHandle,axesH,figH,pp,fit);
    end         
    if ~isempty(currentfit) % update strings in 2nd panel if there
                            % is a current fit
        [coeffresidstrings, err, pp, resid] = bfitcalcfit(newdataHandle,currentfit);
        if err
            error('Data was changed to have repeated values since last selection.')
        end
    elseif isempty(currentfit)
        coeffresidstrings = ' ';  % if current fit is empty, the 2nd panel is empty
    end
    
    % update the legend so it's stuff + fits + evalresults
    legendH = bfitcreatelegend(axesH);
    
    % add equations to plot
    if guistate.equations
        bfitcheckshowequations(guistate.equations, newdataHandle, guistate.digits)
    end
    
    if guistate.plotresids
        % plot resids with other info on plot
        bfitcheckplotresiduals(guistate.plotresids,newdataHandle, ...
            guistate.plottype,~(guistate.subplot),guistate.showresid);
    end
 
	% Recalculate and replot Evaluated results if needed 
    plotresults = getappdata(newdataHandle,'_Basic_Fit_EvalResults'); 
	% only "clearresults" if plotresults.x plotresults.y both empty
	clearresults = isempty(plotresults.x) & isempty(plotresults.y);
	[evalresultsx,evalresultsy] = ...
		bfitevalfitbutton(newdataHandle,currentfit, plotresults.string, guistate.plotresults, clearresults);

	bfitlistenon(figH)
end
