Documentation

matlab.lang.makeValidName

Construct validMATLABidentifiers from input strings

Syntax

N = matlab.lang.makeValidName(S)
N = matlab.lang.makeValidName(S,Name,Value)
[N, modified] = matlab.lang.makeValidName(___)

Description

example

N= matlab.lang.makeValidName(S)constructs valid MATLAB®identifiers,N, from input strings,S. ThemakeValidNamefunction does not guarantee the strings inNare unique.

A valid MATLAB identifier is a character vector of alphanumerics (A–Z, a–z, 0–9) and underscores, such that the first character is a letter and the length of the character vector is less than or equal tonamelengthmax.

makeValidNamedeletes any whitespace characters before replacing any characters that are not alphanumerics or underscores. If a whitespace character is followed by a lowercase letter,makeValidNameconverts the letter to the corresponding uppercase character.

example

N= matlab.lang.makeValidName(S,Name,Value)includes additional options specified by one or moreName,Valuepair arguments.

example

[N,modified] = matlab.lang.makeValidName(___)returns a logical array,modified, indicating modified elements. You can use this syntax with any of the input arguments of the previous syntaxes.

Examples

collapse all

S = {'Item_#','Price/Unit','1st order','Contact'}; N = matlab.lang.makeValidName(S)
N =1x4 cell array{'Item__'} {'Price_Unit'} {'x1stOrder'} {'Contact'}

In the first and second elements,makeValidNamereplaced the invalid characters (#and/), with underscores. In the third element,makeValidNameappended a prefix because the character vector does not begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

Replace invalid characters with the corresponding hexadecimal representation.

S = {'Item_#','Price/Unit','1st order','Contact'}; N = matlab.lang.makeValidName(S,“ReplacementStyle','hex')
N =1x4 cell array{'Item_0x23'} {'Price0x2FUnit'} {'x1stOrder'} {'Contact'}

In the first and second elements,makeValidNamereplaced the invalid characters (#and/), with their hexadecimal representation. In the third element,makeValidNameappended a prefix because the character vector does not begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

Delete invalid characters.

N = matlab.lang.makeValidName(S,“ReplacementStyle','delete')
N =1x4 cell array{'Item_'} {'PriceUnit'} {'x1stOrder'} {'Contact'}

makeValidNamedeleted the invalid characters (#and/). In the third element,makeValidNameappended a prefix because the character vector does not begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

S = {'1stMeasurement','2ndMeasurement','Control'}; N = matlab.lang.makeValidName(S,'Prefix','m_')
N =1x3 cell array{'m_1stMeasurement'} {'m_2ndMeasurement'} {'Control'}

Only the elements that do not start with a letter are prepended with a prefix.

S = {'a%name','name_1','2_name'}; [N, modified] = matlab.lang.makeValidName(S)
N =1x3 cell array{'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.

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside single quotes (' '). You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:“ReplacementStyle','delete'deletes invalid characters.

collapse all

Replacement style, specified as'underscore','delete', or'hex'. The value controls how MATLAB replaces nonalphanumeric characters. For all values ofReplacementStyle, MATLAB deletes whitespace characters and changes a lowercase letter following a whitespace to uppercase.

ReplacementStyleValue Description
'underscore'(default) Replaces all characters that are not alphanumerics or underscores with underscores.'underscore'.
'hex' Replaces each character that is not an alphanumeric or underscore with its corresponding hexadecimal representation.'hex'.
'delete' Deletes all characters that are not alphanumerics or underscores.'delete'.

Characters to prefix to inputs that do not begin with a letter aftermakeValidNamereplaces nonalphanumeric characters, specified as a character vector or string scalar. A valid prefix must start with a letter, contain only alphanumeric characters and underscores, and not be longer than the value ofnamelengthmax.

Output Arguments

collapse all

Valid MATLAB identifiers, returned as a character vector, cell array of character vectors, or string array. The output has the same number of dimensions as the input,S.

修改元素的指标,作为罗技返回cal scalar or array and having the same number of dimensions as the input,S. A value of1(true) indicates thatmakeValidNamemodified the input in the corresponding location. A value of0(false) indicates thatmakeValidNamedid not need to modify the input in the corresponding location.

Tips

  • To ensure that input values are valid and unique, usematlab.lang.makeUniqueStringsaftermatlab.lang.makeValidName.

    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'

  • To customize an invalid character replacement, first use functions such asstrreporregexprepto convert to valid characters. For example, convert'@'characters inSto'At'usingstrrep(S,'@','At'). Then, usematlab.lang.makeValidNameto ensure that all characters inSare valid.

Introduced in R2014a

Was this topic helpful?