function [overwritevar1,overwritevar2,errmsg] = bfitsavetoworkspace(type,savevar1,var1name,var1value,err1,...
    savevar2,var2name,var2value,err2,combovalue,overwriteok)
% BFITSAVETOWORKSPACE Save variables to the workspace. 
%

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.1 $  $Date: 2000/04/28 21:45:29 $

overwritevar1 = 0;
overwritevar2 = 0;
errmsg1 = '';
errmsg2 = '';

if savevar1
    if isvarname(var1name)
        if evalin('base',['exist(''',var1name,''', ''var'');']) & ~overwriteok
            overwritevar1 = 1;
        end
    elseif isempty(var1name)
        errmsg1 = err1.empty;
    else
        errmsg1 = err1.invalid;
    end
end

if savevar2
    if isvarname(var2name)
        if evalin('base',['exist(''',var2name,''', ''var'');']) & ~overwriteok
            overwritevar2 = 1;
        end
    elseif isempty(var2name)
        errmsg2 = err2.empty;
    else
        errmsg2 = err2.invalid;
    end
end

errmsg = [errmsg1 errmsg2];
if ~overwritevar1 & ~overwritevar2 & isempty(errmsg)
    if (savevar1 & savevar2) & isequal(var1name,var2name)
        % save both in same variable name depending on type
        assignin('base',var1name,combovalue);
    else
        if savevar1
            assignin('base',var1name,var1value);
        end
        if savevar2
            assignin('base',var2name,var2value);
        end
    end
    switch type
    case {'fit','results'}
        disp('Basic Fitting GUI created variables in the current workspace.')
    case 'datastats'
        disp('Data Statistics GUI created variables in the current workspace.')
    end
end


