function [X,Y,errmsg] = bfitevalfit(expression,datahandle,fit)
% BFITEVALFIT evaluates a fit.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.9 $  $Date: 2000/06/13 22:05:23 $

X = []; 
Y = []; 
errmsg = '';

try
    % make into array in case a list of numbers
    X = evalin('base',['[' expression ']']);
catch
    errmsg = sprintf('%s\n%s\n\n%s',...
        'Attempt to evalute expression in MATLAB base workspace', ...
        ' failed with the following error:', lasterr);
    errordlg(errmsg);
    return
end

if ~isreal(X)
    warnmsg = sprintf('Imaginary part of expression ignored.');
    warndlg(warnmsg,sprintf('Basic Fitting'));
    lastwarn(warnmsg);
    X = real(X);
end
coeffcell = getappdata(datahandle,'_Basic_Fit_Coeff'); % cell array of pp structures
pp = coeffcell{fit+1};

if isempty(pp)
    error('No fit to evaluate')
end

X = X(:);
% Calculate with "newX" but return with "X": (what we plot, etc with)
guistate = getappdata(datahandle,'_Basic_Fit_Gui_State');
normalized = getappdata(datahandle,'_Basic_Fit_Normalizers');
if guistate.normalize
    newX  = (X - normalized(1))./(normalized(2));
else
    newX = X;
end
switch fit
case {0,1} % spline or pchip
    Y = ppval(pp,newX);
otherwise
    Y = polyval(pp,newX);
end
Y = Y(:);

