www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/mbcMakeValidName.m

    function [names] = mbcMakeValidName(names, varargin)
%MBCMAKEVALIDNAME constructs valid and unique MATLAB identifiers from input
%   N = MBCMAKEVALIDNAME(S) returns valid unique identifiers, N,
%   constructed from the input S. S is specified as a string or a cell
%   array of strings. 
%
%   N = MBCMAKEVALIDNAME(___, PARAM1, VAL1, PARAM2, VAL2) 
%   constructs valid identifiers using additional options specified by one 
%   or two Name, Value pair arguments.
%
%   Parameters include:
%
%   'Prefix'                   Prepends the name when the first character 
%                              is not alphabetical. A valid prefix must
%                              start with a letter and only contain 
%                              alphanumeric characters and underscores.
%
%                              The default 'Prefix' is 'm'.
%
%   'Excluded'                 Makes the strings in N unique among 
%                              themselves and unique with respect to EXCLUDED.
%
%
%   For a detailed information of the sintax of the function, refer to
%   MATLAB.LANG.MAKEVALIDNAME and MATLAB.LANG.MAKEUNIQUESTRINGS help.
%
%   Copyright 2016 The MathWorks, Inc.

% Parse inputs
[names, prefix, excluded] = parseInputs(names, varargin{:});

% Call MATLAB.LANG.MAKEVALIDNAME to construct valid MATLAB identifiers from input
names =   matlab.lang.makeValidName(names,'Prefix', prefix);

% Call MATLAB.LANG.MAKEUNIQUESTRINGS to construct unique and length limited MATLAB identifiers from input
names = matlab.lang.makeUniqueStrings(names, excluded, namelengthmax );


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  INPUT PARSING  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [names, prefix, excluded]  = parseInputs(names, varargin)
persistent parser
if isempty(parser)
    parser = inputParser;
    parser.FunctionName = 'mbcMakeValidName';
    % Set default prefix to m
    parser.addParameter('Prefix','m_'); 
    % Set default excluded list to none
    parser.addParameter('Excluded',{});
end

try
    parser.parse(varargin{:});
catch ME
    throwAsCaller(ME);
end

% Get input parameters from parser object
prefix = parser.Results.Prefix;
excluded = parser.Results.Excluded;