matlab.lang.makeValidName
Converts text into valid variable or field names.
Category: Programming > Functions
Syntax
v = matlab.lang.makeValidName(str)
Returns str changed into a valid name.
[v, modified] = matlab.lang.makeValidName(str)
Also tells which strings had to change.
Description
Valid names are returned unchanged. Spaces are removed and the letter after each is capitalized ('my value' becomes 'myValue'), other illegal characters become underscores, and a name that starts with a digit or is a reserved word gets the prefix x. The function does not make names unique; combine it with matlab.lang.makeUniqueStrings for that.
Tables use this rule to turn column headings of imported files into variable names.
Input arguments
str- Text, string array or cell array of text.
Output arguments
v- Valid names, in the same shape as
str. modified- Logical array:
truewhere the name was changed.
Example
Repairing names
[v, changed] = matlab.lang.makeValidName({'total cost', '3rd', 'a-b', 'ok'})
Output
v = 1×4 cell
{'totalCost'} {'x3rd'} {'a_b'} {'ok'}
changed = 1×4 logical array
1 1 1 0
See also
matlab.lang.makeUniqueStrings isvarname genvarname iskeyword