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,whichStrings) example
  • U = matlab.lang.makeUniqueStrings(S,___, maxStringLength) example
  • [U, modified] = matlab.lang.makeUniqueStrings(___) example

Description

example

U = matlab.lang.makeUniqueStrings(S) constructs unique strings, U, from input strings, S, by appending an underscore and a number to duplicate strings.

example

U = matlab.lang.makeUniqueStrings(S,excludedStrings) constructs strings 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,whichStrings) specifies the subset of S to make unique within the entire set. makeUniqueStrings makes the strings in S(whichStrings) unique among themselves and with respect to the remaining strings. makeUniqueStrings returns the remaining strings unmodified in U. Use this syntax when you have an array of strings, 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 strings in U. If makeUniqeStrings 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 strings.

Examples

expand all

Construct Unique Strings

Create an array of strings and make each element unique.

S = {'John' 'Sue' 'Nick' 'John' 'Campion' 'John' 'Jason'};
U = matlab.lang.makeUniqueStrings(S)
U = 

    'John'    'Sue'    'Nick'    'John_1'    'Campion'    'John_2'    'Jason'

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

Construct Unique Strings and Specify Exclusions

Without specifying excluded strings, make the strings in U unique.

S = {'John' 'Sue' 'Nick' 'John' 'Campion' 'John' 'Jason'};
U = matlab.lang.makeUniqueStrings(S)
U = 

    'John'    'Sue'    'Nick'    'John_1'    'Campion'    'John_2'    'Jason'

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

U = matlab.lang.makeUniqueStrings(S, 'Nick')
U = 

    'John'    'Sue'    'Nick_1'    'John_1'    'Campion'    'John_2'    'Jason'

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

Exclude workspace variables from the unique string array.

Sue = 42;
U = matlab.lang.makeUniqueStrings(S, who)
U = 

    'John'    'Sue_1'    'Nick'    'John_1'    'Campion'    'John_2'    'Jason'

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

Construct Unique Strings for Specified Array Elements

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

S = {'quiz' 'quiz' 'quiz' 'exam' 'quiz' 'exam'};
U = matlab.lang.makeUniqueStrings(S, 1:4)
U = 

    'quiz_1'    'quiz_2'    'quiz_3'    'exam_1'    'quiz'    'exam'

The first four elements in U are unique among themselves, and among the remaining strings 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 = 

    'quiz'    'quiz'    'quiz'    'exam'    'quiz'    'exam'    'quiz'


U = 

    'quiz_1'    'quiz_2'    'quiz_3'    'exam_1'    'quiz'    'exam'    'quiz'

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

Construct Unique Strings with Maximum Length

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 = 

    '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.

Determine Modified Strings

S = {'a%name', 'name_1', '2_name'};
[N, modified] = matlab.lang.makeValidName(S)
N = 

    'a_name'    'name_1'    'x2_name'


modified =

     1     0     1

makeValidName did not modify the second element.

Input Arguments

expand all

S — Input stringsstring or cell array of strings

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

excludedStrings — Strings to excludestring or cell array of strings

Strings to exclude from U, specified as a string or cell array of strings.

Example: 'dontDuplicateThisString', {'excludeS1','excludeS2'}, who

whichStrings — Subset of strings to make uniquerange of linear indices or logical array

Subset of strings, 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 whichStrings.

If whichStrings is a logical array, strings 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]

maxStringLength — Maximum length of output stringsinteger

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

Output Arguments

expand all

U — Unique stringsstring | cell array of strings

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

modified — Indicator of modified stringslogical scalar | logical array

Indicator of modified strings, 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 string in the corresponding location. A value of 0 (false) indicates that makeUniqeStrings did not need to modify the input string in the corresponding location.

More About

expand all

Tips

  • To ensure strings are valid and unique, use matlab.lang.makeValidName before matlab.lang.makeUniqueStrings.

    S = {'my.Name','my_Name','my_Name'};
    validStrings = matlab.lang.makeValidName(S)
    validUniqueStrings = matlab.lang.makeUniqueStrings(validStrings,...
        {},namelengthmax)
    validStrings = 
    
        'my_Name'    'my_Name'    'my_Name'
    
    
    validUniqueStrings = 
    
        'my_Name'    'my_Name_1'    'my_Name_2'
Was this topic helpful?