function oldName = defaultProfile(newName)
%parallel.defaultProfile Get or set the default parallel computing profile
% PROFILE = parallel.defaultProfile returns the name of the
% default profile
%
% OLDPROFILE = parallel.defaultProfile(NEWPROFILE) sets the
% default profile to be NEWPROFILE and returns the previous default
% profile.
%
% If the default profile has been deleted, or if it has never been
% set, parallel.defaultProfile returns 'local' as the default
% profile.
%
% Profiles for cluster types can be created and changed using the
% saveProfile() or saveAsProfile() methods on a cluster object.
% Profiles can be created,
% deleted, and changed using the Cluster Profiles Manager. On the Home
% tab, in the Environment section, click Parallel > Create and Manage
% Clusters... to open the Cluster Profiles Manager.
%
% Examples:
% Display the names of all the available profiles and set the first in the
% list to be the default.
% allNames = parallel.listProfiles()
% parallel.defaultProfile(allNames{1});
%
% First set the profile named 'MyProfile' to be the default and then set
% the profile named 'Profile2' to be the default.
% parallel.defaultProfile('MyProfile');
% oldDefault = parallel.defaultProfile('Profile2');
% strcmp(oldDefault, 'MyProfile') % returns true.
%
% See also PARPOOL, BATCH, PARCLUSTER, parallel.listProfiles.

% Copyright 2011-2022 The MathWorks, Inc.

try
parallel.internal.pool.errorIfPoolThreadWorker(mfilename);
p = parallel.Settings;
oldName = p.DefaultProfile;

if nargin > 0
% Convert any string inputs to character vectors
newName = convertStringsToChars(newName);
% Convert "local" in API to "Processes". "local" will always work both for
% backwards compatibility and as a convenience alias for "Processes".
if strcmp(newName, 'local')
newName = 'Processes';
end
if p.hProfileExists(newName) && parallel.internal.settings.isProfileVisible(newName)
parallel.internal.settings.checkCanSetAsDefault(newName);
p.DefaultProfile = newName;
else
p.hThrowNonExistentProfile(newName);
end
end
catch err
throw(err);
end
end