function [firstname,secondname] = bfitdefaultnames(dialogtype)
% BFITDEFAULTNAMES Creates default variable names for Save to Workspace dialogs.  
% Use 'fit' for 'bfitsavefit'.
% Use 'results' for 'bfitsaveresults'.
% Use 'datastats' for 'bfitsavedatastats'.

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.2 $  $Date: 2000/05/22 01:47:29 $

switch dialogtype
case 'fit'
    name1prefix = 'fit';
    name2prefix = 'normresid';
case 'results'
    name1prefix = 'x';
    name2prefix = 'fx';
case 'datastats'
    name1prefix = 'xstats';
    name2prefix = 'ystats';
otherwise
    error('No such Save to Workspace dialog type.')
end

firstname = computename(name1prefix);
secondname = computename(name2prefix);

%-----------------------------------------------------------------
function name = computename(nameprefix)

% get all names that start with prefix in workspace
workvars = evalin('base', ['char(who(''',nameprefix,'*''))']);
% trim off prefix name
workvars = workvars(:,length(nameprefix)+1:end); 

if ~isempty(workvars)
    % remove all names with suffixes that are "non-numeric"
    lessthanzero = workvars < '0';
    morethannine = workvars > '9';
    notblank = (workvars ~= ' ');
    notnumrows = any((notblank & (lessthanzero | morethannine)),2);
    workvars(notnumrows,:) = [];
end

% find the "next one"
if isempty(workvars)
    name = [nameprefix, '1'];
else
    nextone = max(str2num(workvars)) + 1;
    name = [nameprefix, num2str(nextone)];
end

