time | Calls | line |
---|
| | 153 | function [str, uniquified] = makeUnique(str, stringsToProtect, maxStringLength)
|
< 0.001 | 8 | 154 | szStrings = size(str);
|
< 0.001 | 8 | 155 | numStrings = numel(str);
|
< 0.001 | 8 | 156 | uniquified = false(1, numStrings);
|
< 0.001 | 8 | 157 | [str, sortIdx] = sort(reshape(str, 1, []));
|
< 0.001 | 8 | 158 | stringsToProtect = reshape(stringsToProtect, 1, []);
|
| | 159 |
|
| | 160 | % Find where there are groups of duplicates by comparing two sorted strings
|
| | 161 | % having a one-element shift.
|
0.001 | 8 | 162 | isDuplicateOfPrevious = [false(min(1, numStrings)), strcmp(str(1:end-1), str(2:end))];
|
< 0.001 | 8 | 163 | isDuplicateOfPreviousDiff = diff([isDuplicateOfPrevious, false]);
|
< 0.001 | 8 | 164 | isDuplicateStart = (isDuplicateOfPreviousDiff > 0);
|
< 0.001 | 8 | 165 | isDuplicateStopIdx = find(isDuplicateOfPreviousDiff < 0);
|
| | 166 |
|
| | 167 | % Find where groups of protected strings start.
|
| | 168 | % For performance, only call ismember when there are both elements in
|
| | 169 | % stringsToProtect and duplicates to compare. This is important for the
|
| | 170 | % case when maxStringLength is needed but the second input is {}.
|
< 0.001 | 8 | 171 | isProtectedStart = false(1, numStrings);
|
< 0.001 | 8 | 172 | if ~isempty(stringsToProtect) && any(~isDuplicateOfPrevious)
|
< 0.001 | 8 | 173 | isProtectedStart(~isDuplicateOfPrevious) = matches(str(~isDuplicateOfPrevious), stringsToProtect);
|
< 0.001 | 8 | 174 | end
|
| | 175 |
|
< 0.001 | 8 | 176 | duplicateNum = 1;
|
< 0.001 | 8 | 177 | for changeIdx = find(isDuplicateStart | isProtectedStart)
|
| | 178 | if isDuplicateStart(changeIdx) && isProtectedStart(changeIdx)
|
| | 179 | startIdx = changeIdx;
|
| | 180 | stopIdx = isDuplicateStopIdx(duplicateNum);
|
| | 181 | duplicateNum = duplicateNum + 1;
|
| | 182 | elseif isDuplicateStart(changeIdx)
|
| | 183 | startIdx = changeIdx + 1;
|
| | 184 | stopIdx = isDuplicateStopIdx(duplicateNum);
|
| | 185 | duplicateNum = duplicateNum + 1;
|
| | 186 | else % isProtectedStartIdx(changeIdx)
|
| | 187 | startIdx = changeIdx;
|
| | 188 | stopIdx = changeIdx;
|
| | 189 | end
|
| | 190 | try
|
| | 191 | str(startIdx:stopIdx) = makeNameUnique(str, startIdx, stopIdx, stringsToProtect, maxStringLength);
|
| | 192 | catch ex
|
| | 193 | throwAsCaller(ex);
|
| | 194 | end
|
| | 195 | uniquified(startIdx:stopIdx) = true;
|
| | 196 | end
|
| | 197 |
|
| | 198 | % Unsort and reshape the now unique STRINGS and MODIFIED to match how
|
| | 199 | % STRINGS was input.
|
< 0.001 | 8 | 200 | inverseSortIdx(sortIdx) = 1:numStrings;
|
< 0.001 | 8 | 201 | str = reshape(str(inverseSortIdx), szStrings);
|
< 0.001 | 8 | 202 | uniquified = reshape(uniquified(inverseSortIdx), szStrings);
|
< 0.001 | 8 | 203 | end
|
Other subfunctions in this file are not included in this listing.