# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# matlab.lang.makeUniqueStrings

Construct unique strings from input strings

## Syntax

• ``U = matlab.lang.makeUniqueStrings(S)``
example
• ``U = matlab.lang.makeUniqueStrings(S,excludedStrings)``
example
• ``U = matlab.lang.makeUniqueStrings(S,whichStringsIdx)``
example
• ``U = matlab.lang.makeUniqueStrings(S,___, maxStringLength)``
example
• ``````[U, modified] = matlab.lang.makeUniqueStrings(___)``````
example

## Description

example

````U = matlab.lang.makeUniqueStrings(S)` constructs unique character vectors, `U`, from input character vectors, `S`, by appending an underscore and a number to duplicates. ```

example

````U = matlab.lang.makeUniqueStrings(S,excludedStrings)` constructs character vectors that are unique within `U` and with respect to `excludedStrings`. The `makeUniqueStrings` function does not check `excludedStrings` for uniqueness.```

example

````U = matlab.lang.makeUniqueStrings(S,whichStringsIdx)` specifies the subset of `S` to make unique within the entire set. `makeUniqueStrings` makes the character vectors in `S(whichStringsIdx)` unique among themselves and with respect to the remaining character vectors. `makeUniqueStrings` returns the remaining character vectors unmodified in `U`. Use this syntax when you have an array of character vectors, and need to check that only some elements of the array are unique.```

example

````U = matlab.lang.makeUniqueStrings(S,___, maxStringLength)` specifies the maximum length, `maxStringLength`, of character vectors in `U`. If `makeUniqueStrings` cannot make elements in `S` unique without exceeding `maxStringLength`, it returns an error. You can use this syntax with any of the input arguments of the previous syntaxes.```

example

``````[U, modified] = matlab.lang.makeUniqueStrings(___)``` returns a logical array, `modified`, indicating modified character vectors.```

## Examples

collapse all

Create a cell array of names and make each element unique.

```S = {'John' 'Sue' 'Nick' 'John' 'Campion' 'John' 'Jason'}; U = matlab.lang.makeUniqueStrings(S) ```
```U = 1×7 cell array 'John' 'Sue' 'Nick' 'John_1' 'Campion' 'John_2' 'Jason' ```

The `makeUniqueStrings` function appends the duplicate names in elements 3 and 5 with underscores and incrementing numbers.

Without specifying excluded values, make the character vectors in `U` unique.

```S = {'John' 'Sue' 'Nick' 'John' 'Campion' 'John' 'Jason'}; U = matlab.lang.makeUniqueStrings(S) ```
```U = 1×7 cell array 'John' 'Sue' 'Nick' 'John_1' 'Campion' 'John_2' 'Jason' ```

Specify that the character vector, `'Nick'`, should be excluded from the output.

```U = matlab.lang.makeUniqueStrings(S, 'Nick') ```
```U = 1×7 cell array 'John' 'Sue' 'Nick_1' 'John_1' 'Campion' 'John_2' 'Jason' ```

`makeUniqueStrings` excludes `'Nick'` from `U` and instead modifies the first duplicate, found in element 3, to be `'Nick_1'`.

Exclude workspace variables from the unique cell array.

```Sue = 42; U = matlab.lang.makeUniqueStrings(S, who) ```
```U = 1×7 cell array 'John' 'Sue_1' 'Nick' 'John_1' 'Campion' 'John_2' 'Jason' ```

Since `'Sue'` exists in the workspace, `makeUniqueStrings` makes this character vector unique by appending an underscore and number.

Create an array of character vectors and make only the first four elements unique.

```S = {'quiz' 'quiz' 'quiz' 'exam' 'quiz' 'exam'}; U = matlab.lang.makeUniqueStrings(S, 1:4) ```
```U = 1×6 cell array 'quiz_1' 'quiz_2' 'quiz_3' 'exam_1' 'quiz' 'exam' ```

The first four elements in `U` are unique among themselves, and among the remaining character vectors in elements 5 and 6 (`'quiz'` and `'exam'`). Alternatively, you can use a logical array instead of a range of linear indices to achieve the same results: `U = matlab.lang.makeUniqueStrings(S, [true true true true false false])` or `U = matlab.lang.makeUniqueStrings(S, logical([1 1 1 1 0 0]))`.

Append a duplicate `'quiz'` onto the end of `S` and make the first four elements unique.

```S{end+1} = 'quiz' U = matlab.lang.makeUniqueStrings(S, 1:4) ```
```S = 1×7 cell array 'quiz' 'quiz' 'quiz' 'exam' 'quiz' 'exam' 'quiz' U = 1×7 cell array 'quiz_1' 'quiz_2' 'quiz_3' 'exam_1' 'quiz' 'exam' 'quiz' ```

The character vectors that `makeUniqueStrings` checks are still unique among themselves and among the remaining elements. Since `makeUniqueStrings` does not check any elements after element 4, duplicate character vectors remain.

Create an array from `S` where the first three elements are unique and the maximum length of each string is 5.

```S = {'sampleData' 'sampleData' 'sampleData' 'sampleData'}; U = matlab.lang.makeUniqueStrings(S, 1:3, 5) ```
```U = 1×4 cell array 'sampl' 'sam_1' 'sam_2' 'sampleData' ```

The first element is truncated to 5 characters. The second and third elements are truncated to 3 characters to allow `makeUniqueStrings` to append an underscore and number, and still not exceed 5 characters.

```S = {'a%name', 'name_1', '2_name'}; [N, modified] = matlab.lang.makeValidName(S) ```
```N = 1×3 cell array 'a_name' 'name_1' 'x2_name' modified = 1×3 logical array 1 0 1 ```

`makeValidName` did not modify the second element.

## Input Arguments

collapse all

Input strings, specified as a character vector or cell array of character vectors.

Character vectors to exclude from `U`, specified as a character vector or cell array of character vectors.

Example: `'dontDuplicateThis'`, `{'excludeS1','excludeS2'}`, `who`

Subset of character vectors, `S`, to make unique within the entire set, specified as a range of linear indices or as a logical array with the same size and shape as `S`. If there are duplicates in `S`, the `makeUniqueStrings` function only modifies those specified by `whichStringsIdx`.

If `whichStringsIdx` is a logical array, character vectors are checked for uniqueness when the array element in the same position has a value of `true`.

Example: `1:5`, `logical([1 0 1])`, ```[true false true]```

Maximum length of output character vectors in `U`, specified as an integer. If `makeUniqueStrings` cannot make elements in `S` unique without exceeding `maxStringLength`, it returns an error.

## Output Arguments

collapse all

Unique character vectors, returned as a character vector or cell array of character vectors. The output has the same dimension as the input, `S`.

Indicator of modified character vectors, returned as a logical scalar or array and having the same dimension as the input, `S`. A value of `1` (`true`) indicates that `makeUniqeStrings` modified the input character vectors in the corresponding location. A value of `0` (`false`) indicates that `makeUniqeStrings` did not need to modify the input character vectors in the corresponding location.

collapse all

### Tips

• To ensure character vectors are valid and unique, use `matlab.lang.makeValidName` before `matlab.lang.makeUniqueStrings`.

```S = {'my.Name','my_Name','my_Name'}; validValues = matlab.lang.makeValidName(S) validUniqueValues = matlab.lang.makeUniqueStrings(validValues,... {},namelengthmax)```
```validValues = 'my_Name' 'my_Name' 'my_Name' validUniqueValues = 'my_Name' 'my_Name_1' 'my_Name_2'```