time | Calls | line |
---|
| | 1 | function [names, modified] = makeValidName(names, varargin)
|
| | 2 | %MATLAB.LANG.MAKEVALIDNAME constructs valid MATLAB identifiers from input S
|
| | 3 | % N = MATLAB.LANG.MAKEVALIDNAME(S) returns valid identifiers, N,
|
| | 4 | % constructed from the input S. S is specified as a character vector, a
|
| | 5 | % cell array of character vectors, or a string array. The values in N are
|
| | 6 | % NOT guaranteed to be unique.
|
| | 7 | %
|
| | 8 | % A valid MATLAB identifier consists of alphanumerics and underscores,
|
| | 9 | % such that the first character is a letter and the length is at most
|
| | 10 | % NAMELENGTHMAX.
|
| | 11 | %
|
| | 12 | % MATLAB.LANG.MAKEVALIDNAME deletes whitespace characters prior to
|
| | 13 | % replacing any characters that are not alphnumerics or underscores. If a
|
| | 14 | % whitespace character is followed by a lowercase letter,
|
| | 15 | % MATLAB.LANG.MAKEVALIDNAME converts the letter to the corresponding
|
| | 16 | % uppercase character.
|
| | 17 | %
|
| | 18 | % N = MATLAB.LANG.MAKEVALIDNAME(___, PARAM1, VAL1, PARAM2, VAL2, ...)
|
| | 19 | % constructs valid identifiers using additional options specified by one
|
| | 20 | % or more Name, Value pair arguments.
|
| | 21 | %
|
| | 22 | % Parameters include:
|
| | 23 | %
|
| | 24 | % 'ReplacementStyle' Controls how non-alphanumeric characters
|
| | 25 | % are replaced. Valid values are 'underscore',
|
| | 26 | % 'hex', and 'delete'.
|
| | 27 | %
|
| | 28 | % 'underscore' indicates non-alphanumeric
|
| | 29 | % characters are replaced with underscores.
|
| | 30 | %
|
| | 31 | % 'hex' indicates each non-alphanumeric
|
| | 32 | % character is replaced with a corresponding
|
| | 33 | % hexadecimal representation.
|
| | 34 | %
|
| | 35 | % 'delete' indicates all non-alphanumeric
|
| | 36 | % characters are deleted.
|
| | 37 | %
|
| | 38 | % The default 'ReplacementStyle' is
|
| | 39 | % 'underscore'.
|
| | 40 | %
|
| | 41 | % 'Prefix' Prepends the name when the first character
|
| | 42 | % is not alphabetical. A valid prefix must
|
| | 43 | % start with a letter, must contain only
|
| | 44 | % alphanumeric characters and underscores,
|
| | 45 | % must not be a MATLAB keyword, and must not
|
| | 46 | % be longer than the value of NAMELENGTHMAX.
|
| | 47 | %
|
| | 48 | % The default 'Prefix' is 'x'.
|
| | 49 | %
|
| | 50 | % [N, MODIFIED] = MATLAB.LANG.MAKEVALIDNAME(S, ___) also returns a
|
| | 51 | % logical array the same size as S, MODIFIED, that denotes modified
|
| | 52 | % elements.
|
| | 53 | %
|
| | 54 | % Examples
|
| | 55 | % --------
|
| | 56 | % Make valid MATLAB identifiers from input character vectors
|
| | 57 | %
|
| | 58 | % S = {'Item_#','Price/Unit','1st order','Contact'};
|
| | 59 | % N = MATLAB.LANG.MAKEVALIDNAME(S)
|
| | 60 | %
|
| | 61 | % returns the cell array {'Item__' 'Price_Unit' 'x1stOrder' 'Contact'}
|
| | 62 | %
|
| | 63 | % Make valid MATLAB identifiers using specified replacement style
|
| | 64 | %
|
| | 65 | % S = {'Item_#','Price/Unit','1st order','Contact'};
|
| | 66 | % N = MATLAB.LANG.MAKEVALIDNAME(S, 'ReplacementStyle', 'delete')
|
| | 67 | %
|
| | 68 | % returns the cell array {'Item_' 'PriceUnit' 'x1stOrder' 'Contact'}
|
| | 69 | %
|
| | 70 | % See also MATLAB.LANG.MAKEUNIQUESTRINGS, ISVARNAME, ISKEYWORD,
|
| | 71 | % ISLETTER, NAMELENGTHMAX, WHO, STRREP, REGEXP, REGEXPREP
|
| | 72 |
|
| | 73 | % Copyright 2013-2018 The MathWorks, Inc.
|
| | 74 |
|
| | 75 | % Parse optional inputs.
|
< 0.001 | 6 | 76 | replacementStyle = "underscore";
|
< 0.001 | 6 | 77 | prefix = 'x';
|
< 0.001 | 6 | 78 | if nargin > 1
|
| | 79 | [replacementStyle, prefix] = parseInputs(replacementStyle,prefix,varargin{:});
|
< 0.001 | 6 | 80 | end
|
| | 81 |
|
| | 82 | % NAMES must be char, cell array, or string array (with no missing values).
|
< 0.001 | 6 | 83 | inputIsChar = false;
|
< 0.001 | 6 | 84 | if ischar(names)
|
| | 85 | inputIsChar = true;
|
| | 86 | names = {names}; % Wrap char NAMES in a cell for algorithm convenience.
|
< 0.001 | 6 | 87 | elseif iscell(names)
|
| | 88 | % Deeper validation of each name is done below.
|
| | 89 | elseif ~isstring(names)
|
| | 90 | error(message('MATLAB:makeValidName:InvalidCandidateNames'));
|
| | 91 | elseif any(ismissing(names(:)))
|
| | 92 | error(message('MATLAB:makeValidName:MissingNames', ...
|
| | 93 | getString(message('MATLAB:string:MissingDisplayText'))));
|
< 0.001 | 6 | 94 | end
|
| | 95 |
|
| | 96 | % Make all the names valid identifiers.
|
< 0.001 | 6 | 97 | modified = false(size(names));
|
< 0.001 | 6 | 98 | for idx = 1:numel(names)
|
| | 99 | % NAMES is either a cell or string array by now. If it is a cell array,
|
| | 100 | % we still need to verify that each name is a char row vector or ''.
|
| | 101 | % In either case, extract one name with braces as a char row.
|
< 0.001 | 16 | 102 | name = names{idx};
|
< 0.001 | 16 | 103 | if iscell(names) && ~(ischar(name) && (isrow(name) || isequal(name,'')))
|
| | 104 | error(message('MATLAB:makeValidName:InvalidCandidateNames'));
|
< 0.001 | 16 | 105 | end
|
< 0.001 | 16 | 106 | modified(idx) = ~isvarname(name);
|
< 0.001 | 16 | 107 | end
|
< 0.001 | 6 | 108 | if any(modified(:))
|
| | 109 | % Get function handle to makeValid for the specified options.
|
0.001 | 5 | 110 | makeValidFcnHandle = getMakeValidFcnHandle(replacementStyle, prefix);
|
0.011 | 5 | 111 | names(modified) = makeValidFcnHandle(names(modified));
|
< 0.001 | 6 | 112 | end
|
| | 113 |
|
| | 114 | % Return NAMES as a char if it was input as one.
|
< 0.001 | 6 | 115 | if inputIsChar
|
| | 116 | names = names{1};
|
< 0.001 | 6 | 117 | end
|
| | 118 |
|
< 0.001 | 6 | 119 | end
|
Other subfunctions in this file are not included in this listing.