Construct unique strings from input strings (2024)

Main Content

Construct unique strings from input strings

collapse all in page


U = matlab.lang.makeUniqueStrings(S)

U = matlab.lang.makeUniqueStrings(S,excludedStrings)

U = matlab.lang.makeUniqueStrings(S,whichStringsIdx)

U = matlab.lang.makeUniqueStrings(S,___, maxStringLength)

[U, modified]= matlab.lang.makeUniqueStrings(___)



U = matlab.lang.makeUniqueStrings(S) constructsunique strings or character vectors, U, from inputstrings or character vectors, S, by appending anunderscore and a number to duplicates.


U = matlab.lang.makeUniqueStrings(S,excludedStrings) constructsstrings or character vectors that are unique within U andwith respect to excludedStrings. The makeUniqueStrings functiondoes not check excludedStrings for uniqueness.


U = matlab.lang.makeUniqueStrings(S,whichStringsIdx) specifiesthe subset of S to make unique within the entireset. makeUniqueStrings makes the elements in S(whichStringsIdx) uniqueamong themselves and with respect to the remaining elements. makeUniqueStrings returnsthe remaining elements unmodified in U. Use thissyntax when you have an string array or an array of character vectors,and need to check that only some elements are unique.


U = matlab.lang.makeUniqueStrings(S,___, maxStringLength) specifiesthe maximum length, maxStringLength, of elementsin U. If makeUniqueStrings cannotmake elements in S unique without exceeding maxStringLength,it returns an error. You can use this syntax with any of the inputarguments of the previous syntaxes.


[U, modified]= matlab.lang.makeUniqueStrings(___) returnsa logical array, modified, indicating the modifiedelements.


collapse all

Construct Unique Character Vectors

Open Live Script

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

Construct Unique Character Vectors and Specify Exclusions

Open Live Script

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 = 1x7 cell {'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 = 1x7 cell {'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 = 1x7 cell {'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.

Construct Unique Elements for Specified Array Indices

Open Live Script

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 cell {'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'
S = 1x7 cell {'quiz'} {'quiz'} {'quiz'} {'exam'} {'quiz'} {'exam'} {'quiz'}
U = matlab.lang.makeUniqueStrings(S, 1:4)
U = 1x7 cell {'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.

Construct Unique Character Vectors with Maximum Length

Open Live Script

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 = 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 allow makeUniqueStrings to append an underscore and number, and still not exceed 5 characters.

Determine Modified Strings

Open Live Script

Create a cell array of names and make each element unique. The output modified shows which elements of the input have been modified.

S = ["John" "Sue" "Nick" "John" "Campion" "John" "Jason"];[U, modified] = matlab.lang.makeUniqueStrings(S)
U = 1x7 string "John" "Sue" "Nick" "John_1" "Campion" "John_2" "Jason"
modified = 1x7 logical array 0 0 0 1 0 1 0

Input Arguments

collapse all

SInput values
character vector | cell array of character vectors | string array

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

excludedStringsStrings to exclude
character vector | cell array of character vectors | string array

Character vectors to exclude from U, specifiedas a character vector, cell array of character vectors, or stringarray.

Example: 'dontDuplicateThis'

Example: {'excludeS1' 'excludeS2'}

Example: ["excludeThis" "andThis"]

Example: who

whichStringsIdxSubset of strings to make unique
range of linear indices or logical array

Subset of Sto make unique within the entireset, specified as a range of linear indices or as a logical arraywith the same size and shape as S. If there areduplicates in S, the makeUniqueStrings functiononly modifies those specified by whichStringsIdx.

If whichStringsIdx is a logical array, elementsare checked for uniqueness when the array element in the same positionhas a value of true.

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

maxStringLengthMaximum length of strings

Maximum length of strings in U, specifiedas an integer. If makeUniqueStrings cannot makeelements in S unique without exceeding maxStringLength,it returns an error.

Output Arguments

collapse all

U — Unique strings
character vector | cell array of character vectors | string array

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

modified — Indicator of modified elements
logical scalar | logical array

Indicator of modified elements, returned as a logical scalaror array and having the same dimension as the input, S.A value of 1 (true) indicatesthat makeUniqueStrings modified the element inthe corresponding location. A value of 0 (false)indicates that makeUniqueStrings did not need tomodify the element in the corresponding location.


  • To ensure that input values 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'

Version History

Introduced in R2014a

See Also

matlab.lang.makeValidName | namelengthmax | who

MATLAB Command

You clicked a link that corresponds to this MATLAB command:


Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Construct unique strings from input strings (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:


  • América Latina (Español)
  • Canada (English)
  • United States (English)


  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Construct unique strings from input strings (2024)
Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5727

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.