matlab.lang.makeValidName

Construct valid MATLAB identifiers from input strings

Syntax

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

Description

example

N = matlab.lang.makeValidName(S) constructs valid MATLAB® identifiers, N, from input strings, S. The makeValidName function does not guarantee the strings in N are unique.

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

makeValidName deletes any whitespace characters prior to replacing any characters that are not alphanumerics or underscores. If a whitespace character is followed by a lowercase letter, makeValidName converts the letter to the corresponding uppercase character.

example

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

example

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

Examples

expand all

Construct Valid MATLAB Identifiers

S = {'Item_#','Price/Unit','1st order','Contact'};
N = matlab.lang.makeValidName(S)
N = 

    'Item__'    'Price_Unit'    'x1stOrder'    'Contact'

In the first and second strings, makeValidName replaced the invalid characters (# and /), with underscores. In the third string, makeValidName appended a prefix because the string doesn't begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

Construct Valid MATLAB Identifiers Using Specified Replacement Style

Replace invalid characters with the corresponding hexadecimal representation.

S = {'Item_#','Price/Unit','1st order','Contact'};
N = matlab.lang.makeValidName(S,'ReplacementStyle','hex')
N = 

    'Item_0x23'    'Price0x2FUnit'    'x1stOrder'    'Contact'

In the first and second strings, makeValidName replaced the invalid characters (# and /), with their hexadecimal representation. In the third string, makeValidName appended a prefix because the string doesn't 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 = 

    'Item_'    'PriceUnit'    'x1stOrder'    'Contact'

makeValidName deleted the invalid characters (# and /). In the third string, makeValidName appended a prefix because the string doesn't begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

Construct Valid MATLAB Identifiers Using Specified Prefix

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

    'm_1stMeasurement'    'm_2ndMeasurement'    'Control'

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

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. If S is a cell array of strings, it must be a 1xN or Nx1 cell array.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

'ReplacementStyle' — Replacement style'underscore' (default) | 'delete' | 'hex'

Replacement style, specified as a string. The value controls how MATLAB replaces nonalphanumeric characters.

ReplacementStyle ValueDescription
'underscore' (default)Replaces all characters that are not alphanumerics or underscores with underscores. 'underscore' deletes whitespace characters and changes a lowercase letter following a whitespace to uppercase.
'hex'Replaces each character that is not an alphanumeric or underscore with its corresponding hexadecimal representation. 'hex' deletes whitespace characters and changes a lowercase letter following a whitespace to uppercase.
'delete'Deletes all characters that are not alphanumerics or underscores. 'delete' deletes whitespace characters and changes any lowercase letter following a whitespace to uppercase.

'Prefix' — Character to prefix'x' (default) | string

Character to prefix to input strings that do not begin with a letter after makeValidName replaces nonalphanumeric characters, specified as a string. A valid prefix must start with a letter, contain only alphanumeric characters and underscores, and not be longer than the value of namelengthmax.

Output Arguments

expand all

N — Valid MATLAB identifiersstring | cell array of strings

Valid MATLAB identifiers, returned as a string or cell array of strings. The output has the same number of dimensions 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 number of dimensions as the input, S. A value of 1 (true) indicates that makeValidName modified the input string in the corresponding location. A value of 0 (false) indicates that makeValidName 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.makeUniqueStrings after matlab.lang.makeValidName.

    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'
  • To customize an invalid character replacement, first use functions such as strrep or regexprep to convert to valid characters. For example, convert '@' characters in the string, S, to 'At' using strrep(S,'@','At'). Then, use matlab.lang.makeValidName to ensure that all characters in the strings are valid.

Was this topic helpful?