Main Content

matlab.lang.makeUniqueStrings

Construct unique strings from input strings

Description

example

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

example

U = matlab.lang。makeUniqueStrings(S,excludedStrings)constructs strings or character vectors that are unique withinUand with respect toexcludedStrings. ThemakeUniqueStringsfunction does not checkexcludedStringsfor uniqueness.

example

U = matlab.lang。makeUniqueStrings(S,whichStringsIdx)specifies the subset ofSto make unique within the entire set.makeUniqueStringsmakes the elements inS(whichStringsIdx)unique among themselves and with respect to the remaining elements.makeUniqueStringsreturns the remaining elements unmodified inU. Use this syntax when you have an string array or an array of character vectors, and need to check that only some elements are unique.

example

U = matlab.lang。makeUniqueStrings(S,___,maxStringLength)specifies the maximum length,maxStringLength, of elements inU. IfmakeUniqueStringscannot make elements inSunique without exceedingmaxStringLength, 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 the modified elements.

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 =1x7 cellColumns 1 through 6 {'John'} {'Sue'} {'Nick'} {'John_1'} {'Campion'} {'John_2'} Column 7 {'Jason'}

ThemakeUniqueStringsfunction appends the duplicate names in elements 3 and 5 with underscores and incrementing numbers.

Without specifying excluded values, make the character vectors inUunique.

S = {'John''Sue''Nick''John''Campion''John''Jason'}; U = matlab.lang.makeUniqueStrings(S)
U =1x7 cellColumns 1 through 6 {'John'} {'Sue'} {'Nick'} {'John_1'} {'Campion'} {'John_2'} Column 7 {'Jason'}

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

U = matlab.lang。makeUniqueStrings(S,'Nick')
U =1x7 cellColumns 1 through 5 {'John'} {'Sue'} {'Nick_1'} {'John_1'} {'Campion'} Columns 6 through 7 {'John_2'} {'Jason'}

makeUniqueStringsexcludes'Nick'fromUand instead modifies the first duplicate, found in element 3, to be'Nick_1'.

Exclude workspace variables from the unique cell array.

苏= 42;U = matlab.lang。makeUniqueStrings (S)
U =1x7 cellColumns 1 through 5 {'John'} {'Sue_1'} {'Nick'} {'John_1'} {'Campion'} Columns 6 through 7 {'John_2'} {'Jason'}

Since'Sue'exists in the workspace,makeUniqueStringsmakes 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 =1x6 cellColumns 1 through 5 {'quiz_1'} {'quiz_2'} {'quiz_3'} {'exam_1'} {'quiz'} Column 6 {'exam'}

The first four elements inUare 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])orU = matlab.lang。makeUniqueStrings(S, logical([1 1 1 1 0 0])).

Append a duplicate'quiz'onto the end ofSand make the first four elements unique.

S{end+1} ='quiz'
S =1x7 cellColumns 1 through 6 {'quiz'} {'quiz'} {'quiz'} {'exam'} {'quiz'} {'exam'} Column 7 {'quiz'}
U = matlab.lang。makeUniqueStrings(S, 1:4)
U =1x7 cellColumns 1 through 5 {'quiz_1'} {'quiz_2'} {'quiz_3'} {'exam_1'} {'quiz'} Columns 6 through 7 {'exam'} {'quiz'}

The character vectors thatmakeUniqueStringschecks are still unique among themselves and among the remaining elements. SincemakeUniqueStringsdoes not check any elements after element 4, duplicate character vectors remain.

Create an array fromSwhere 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 =1x4 cell{'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 allowmakeUniqueStringsto 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 =1x3 cell{'a_name'} {'name_1'} {'x2_name'}
modified =1x3 logical array1 0 1

makeValidNamedid not modify the second element.

Input Arguments

collapse all

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

Character vectors to exclude fromU, specified as a character vector, cell array of character vectors, or string array.

Example:'dontDuplicateThis'

Example:{'excludeS1' 'excludeS2'}

Example:["excludeThis" "andThis"]

Example:who

Subset ofSto make unique within the entire set, specified as a range of linear indices or as a logical array with the same size and shape asS. If there are duplicates inS,makeUniqueStringsfunction only modifies those specified bywhichStringsIdx.

IfwhichStringsIdxis a logical array, elements are checked for uniqueness when the array element in the same position has a value oftrue.

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

Maximum length of strings inU, specified as an integer. IfmakeUniqueStringscannot make elements inSunique without exceedingmaxStringLength, it returns an error.

Output Arguments

collapse all

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

Indicator of modified elements, returned as a logical scalar or array and having the same dimension as the input,S. A value of1(true) indicates thatmakeUniqueStringsmodified the element in the corresponding location. A value of0(false) indicates thatmakeUniqueStringsdid not need to modify the element in the corresponding location.

Tips

  • To ensure that input values are valid and unique, usematlab.lang.makeValidNamebeforematlab.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'

Introduced in R2014a