matlab.lang.makeValidName - Construct valid MATLAB identifiers from input strings - MATLAB (original) (raw)
Construct valid MATLAB identifiers from input strings
Syntax
Description
[N](#bt5g4k9-1-N) = matlab.lang.makeValidName([S](#bt5g4k9-1-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 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 to namelengthmax
.
makeValidName
deletes any whitespace characters before 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.
[N](#bt5g4k9-1-N) = matlab.lang.makeValidName([S](#bt5g4k9-1-S),[Name,Value](#namevaluepairarguments))
includes additional options specified by one or more Name,Value
pair arguments.
[[N](#bt5g4k9-1-N), [modified](#bt5g4k9-1-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
S = {'Item_#','Price/Unit','1st order','Contact'}; N = matlab.lang.makeValidName(S)
N = 1×4 cell {'Item__'} {'Price_Unit'} {'x1stOrder'} {'Contact'}
In the first and second elements, makeValidName
replaced the invalid characters (#
and /
), with underscores. In the third element, makeValidName
appended 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 = 1×4 cell {'Item_0x23'} {'Price0x2FUnit'} {'x1stOrder'} {'Contact'}
In the first and second elements, makeValidName
replaced the invalid characters (#
and /
), with their hexadecimal representation. In the third element, makeValidName
appended 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 = 1×4 cell {'Item_'} {'PriceUnit'} {'x1stOrder'} {'Contact'}
makeValidName
deleted the invalid characters (#
and /
). In the third element, makeValidName
appended 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 = 1×3 cell {'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 = 1×3 cell {'a_name'} {'name_1'} {'x2_name'}
modified = 1×3 logical array
1 0 1
makeValidName
did not modify the second element.
Input Arguments
Input strings, specified as a character vector, cell array of character vectors, or string array.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: 'ReplacementStyle','delete'
deletes invalid characters.
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.
ReplacementStyle Value | 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 after makeValidName
replaces nonalphanumeric characters, specified as a character vector or string scalar. For example, by default, makeValidName
prefixes characters to an input '*hello'
because, after it replaces nonalphanumeric characters, the input does not begin with a letter ('_hello'
). However, if you specify a replacement style that deletes nonalphanumeric characters, makeValidName
does not prefix characters. After it replaces nonalphanumeric characters, the input begins with a letter ('hello'
).
A valid prefix must meet the following conditions.
- Start with a letter.
- Contain only alphanumeric characters and underscores.
- Not be a MATLAB keyword.
- Not be longer than the value of
namelengthmax
.
Output Arguments
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
.
Indicator of modified elements, returned as a logical scalar or array and having the same number of dimensions as the input, S
. A value of 1
(true
) indicates thatmakeValidName
modified the input in the corresponding location. A value of 0
(false
) indicates that makeValidName
did not need to modify the input in the corresponding location.
Tips
- To ensure that input values are valid and unique, use
matlab.lang.makeUniqueStrings
aftermatlab.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 as
strrep
orregexprep
to convert to valid characters. For example, convert'@'
characters inS
to'At'
usingstrrep(S,'@','At')
. Then, usematlab.lang.makeValidName
to ensure that all characters inS
are valid.
Version History
Introduced in R2014a
The maximum allowed identifier length is increased to 2048. This change allows variables, functions, classes, and many other entities to have names of up to 2048 characters. Longer identifiers negatively impact performance and memory.