time | Calls | line |
---|
| | 1 | function [uniqueStrings, modified] = makeUniqueStrings(inStr, excludes, maxStringLength)
|
| | 2 | %MATLAB.LANG.MAKEUNIQUESTRINGS Constructs unique strings from input strings
|
| | 3 | % U = MATLAB.LANG.MAKEUNIQUESTRINGS(S) constructs unique elements,
|
| | 4 | % U, from S by appending an underscore and a number to duplicates. S
|
| | 5 | % is a character vector (i.e. a 1xN character array or ''), a cell array
|
| | 6 | % of character vectors, or a string array.
|
| | 7 | %
|
| | 8 | % U = MATLAB.LANG.MAKEUNIQUESTRINGS(S, EXCLUDED) makes the elements
|
| | 9 | % in S unique among themselves and unique with respect to EXCLUDED.
|
| | 10 | % MAKEUNIQUESTRINGS does not check EXCLUDED for uniqueness.
|
| | 11 | %
|
| | 12 | % U = MATLAB.LANG.MAKEUNIQUESTRINGS(S, WHICHSTRINGS) makes the elements
|
| | 13 | % in S(WHICHSTRINGS) unique among themselves and with respect to the
|
| | 14 | % remaining elements. WHICHSTRINGS is a logical vector or a vector of
|
| | 15 | % indices. MAKEUNIQUESTRINGS does not check the remaining elements for
|
| | 16 | % uniqueness, and returns them unmodified in U. Use this syntax when you
|
| | 17 | % have a cell or string array and need to check that only some elements
|
| | 18 | % of the array are unique.
|
| | 19 | %
|
| | 20 | % U = MATLAB.LANG.MAKEUNIQUESTRINGS(S, ___, MAXSTRINGLENGTH)
|
| | 21 | % specifies the maximum strlength, MAXSTRINGLENGTH, of the elements in U.
|
| | 22 | % If MAKEUNIQUESTRINGS cannot make elements in S unique without exceeding
|
| | 23 | % MAXSTRINGLENGTH, it throws an error.
|
| | 24 | %
|
| | 25 | % [U, MODIFIED] = MATLAB.LANG.MAKEUNIQUESTRINGS(S, ___) also returns a
|
| | 26 | % logical array the same size as S to denote modified elements.
|
| | 27 | %
|
| | 28 | % Examples
|
| | 29 | % --------
|
| | 30 | % Make unique character vectors with respect to workspace variable names
|
| | 31 | %
|
| | 32 | % var3 = 0;
|
| | 33 | % varNames = MATLAB.LANG.MAKEUNIQUESTRINGS({'var' 'var2' 'var' 'var3'}, who)
|
| | 34 | %
|
| | 35 | % returns the cell array {'var' 'var2' 'var_1' 'var3_1'}
|
| | 36 | %
|
| | 37 | % Make unique character vectors by checking only some elements of the array
|
| | 38 | %
|
| | 39 | % names = {'Peter' 'Jeremy' 'Campion' 'Nick' 'Nick'};
|
| | 40 | % names = MATLAB.LANG.MAKEUNIQUESTRINGS(names, [2 4])
|
| | 41 | %
|
| | 42 | % returns the cell array {'Peter' 'Jeremy' 'Campion' 'Nick_1' 'Nick'}
|
| | 43 | %
|
| | 44 | % See also MATLAB.LANG.MAKEVALIDNAME, NAMELENGTHMAX, WHO.
|
| | 45 |
|
| | 46 | % Copyright 2013-2019 The MathWorks, Inc.
|
| | 47 |
|
| | 48 | % Validate number of inputs.
|
< 0.001 | 8 | 49 | narginchk(1,3);
|
| | 50 |
|
| | 51 | % Validate input string.
|
< 0.001 | 8 | 52 | inputIsChar = false;
|
< 0.001 | 8 | 53 | inputIsCell = false;
|
< 0.001 | 8 | 54 | if matlab.internal.datatypes.isCharString(inStr)
|
| | 55 | inputIsChar = true;
|
< 0.001 | 8 | 56 | elseif matlab.internal.datatypes.isCharStrings(inStr, true)
|
< 0.001 | 8 | 57 | inputIsCell = true;
|
| | 58 | elseif ~isstring(inStr)
|
| | 59 | error(message('MATLAB:makeUniqueStrings:InvalidInputStrings'))
|
| | 60 | elseif any(ismissing(inStr(:)))
|
| | 61 | error(message('MATLAB:makeUniqueStrings:MissingNames', ...
|
| | 62 | getString(message('MATLAB:string:MissingDisplayText'))));
|
< 0.001 | 8 | 63 | end
|
| | 64 |
|
< 0.001 | 8 | 65 | if isempty(inStr)
|
| | 66 | uniqueStrings = inStr;
|
| | 67 | if inputIsChar
|
| | 68 | modified = false;
|
| | 69 | else
|
| | 70 | modified = false(size(inStr));
|
| | 71 | end
|
| | 72 | return;
|
< 0.001 | 8 | 73 | end
|
< 0.001 | 8 | 74 | inStr = string(inStr);
|
| | 75 |
|
| | 76 | % Set/validate EXCLSTRORELEMTOCHK and MAXSTRINGLENGTH.
|
< 0.001 | 8 | 77 | [~, maxArraySize] = computer;
|
< 0.001 | 8 | 78 | if nargin < 3
|
| | 79 | maxStringLength = maxArraySize;
|
< 0.001 | 8 | 80 | else
|
0.002 | 8 | 81 | maxStringLength = validateMaxStringLength(maxStringLength, maxArraySize);
|
< 0.001 | 8 | 82 | end
|
| | 83 |
|
< 0.001 | 8 | 84 | if nargin < 2
|
| | 85 | exclStrOrElemToChk = string({});
|
< 0.001 | 8 | 86 | else
|
0.002 | 8 | 87 | exclStrOrElemToChk = validateExclStrOrElemToChk(excludes, inStr);
|
< 0.001 | 8 | 88 | end
|
| | 89 |
|
| | 90 | % Process differently for 2nd option as checkElements or stringsToProtect.
|
< 0.001 | 8 | 91 | if isnumeric(exclStrOrElemToChk) || islogical(exclStrOrElemToChk) % checkElements
|
| | 92 |
|
| | 93 | % Construct stringsToCheck from STRINGS that need to be made unique.
|
| | 94 | stringsToCheck = inStr(exclStrOrElemToChk);
|
| | 95 |
|
| | 96 | % Truncate only the stringsToCheck.
|
| | 97 | if nargout > 1
|
| | 98 | truncated = false(size(inStr));
|
| | 99 | end
|
| | 100 | if maxStringLength < maxArraySize
|
| | 101 | [stringsToCheck, truncated(exclStrOrElemToChk)] = truncateString(stringsToCheck, maxStringLength);
|
| | 102 | end
|
| | 103 |
|
| | 104 | % Construct stringsToProtect from STRINGS that should not be modified.
|
| | 105 | if islogical(exclStrOrElemToChk)
|
| | 106 | stringsToProtect = inStr(~exclStrOrElemToChk);
|
| | 107 | else % exclStrOrElemToChk is indices
|
| | 108 | stringsToProtect = inStr(setdiff(1:numel(inStr),exclStrOrElemToChk));
|
| | 109 | end
|
| | 110 |
|
| | 111 | % Make stringsToCheck unique against itself and stringsToProtect.
|
| | 112 | [stringsChecked, modifiedInStringsChecked] = ...
|
| | 113 | makeUnique(stringsToCheck, stringsToProtect, maxStringLength);
|
| | 114 |
|
| | 115 | % Combine the protected subset of strings with the checked subset.
|
| | 116 | uniqueStrings = inStr;
|
| | 117 | uniqueStrings(exclStrOrElemToChk) = stringsChecked;
|
| | 118 |
|
| | 119 | % Compute the positions of modified strings in the now completed set.
|
| | 120 | if nargout > 1
|
| | 121 | uniquified = false(size(inStr));
|
| | 122 | uniquified(exclStrOrElemToChk) = modifiedInStringsChecked;
|
| | 123 | end
|
< 0.001 | 8 | 124 | else % stringsToProtect
|
< 0.001 | 8 | 125 | if maxStringLength < maxArraySize
|
0.002 | 8 | 126 | [inStr, truncated] = truncateString(inStr, maxStringLength);
|
| | 127 | elseif nargout > 1
|
| | 128 | truncated = false(size(inStr));
|
< 0.001 | 8 | 129 | end
|
0.007 | 8 | 130 | [uniqueStrings, uniquified] = ...
|
| 8 | 131 | makeUnique(inStr, exclStrOrElemToChk, maxStringLength);
|
< 0.001 | 8 | 132 | end
|
| | 133 |
|
< 0.001 | 8 | 134 | if maxStringLength == 0 && inputIsChar
|
| | 135 | uniqueStrings = char(zeros(1, 0));
|
| | 136 | modified = true;
|
| | 137 | return;
|
< 0.001 | 8 | 138 | end
|
| | 139 |
|
< 0.001 | 8 | 140 | if inputIsChar
|
| | 141 | uniqueStrings = char(uniqueStrings);
|
< 0.001 | 8 | 142 | elseif inputIsCell
|
< 0.001 | 8 | 143 | uniqueStrings = cellstr(uniqueStrings);
|
< 0.001 | 8 | 144 | end
|
| | 145 |
|
< 0.001 | 8 | 146 | if nargout > 1
|
< 0.001 | 8 | 147 | modified = truncated | uniquified;
|
< 0.001 | 8 | 148 | end
|
| | 149 |
|
< 0.001 | 8 | 150 | end
|
Other subfunctions in this file are not included in this listing.