Main Content

genvarname

(To be removed) Construct valid variable name from string

genvarname will be removed in a future release. Use matlab.lang.makeValidName and matlab.lang.makeUniqueStrings instead.

Description

example

varname = genvarname(str) constructs a string or character vector varname that is similar to or the same as the str input, and can be used as a valid variable name. If str is a string array or cell array of character vectors, genvarname returns a string array or cell array of character vectors in varname. The elements returned by genvarname are guaranteed to be different from each other.

Note

genvarname does not create a variable in the MATLAB® workspace. You cannot, therefore, assign a value to the output of genvarname.

varname = genvarname(str,exclusions) returns a valid variable name that is different from any name listed in the exclusions input.

Specify the function who in exclusions to create a variable name that will be unique in the current MATLAB workspace.

Examples

collapse all

Create four similar variable names that do not conflict with each other.

v = genvarname({'A', 'A', 'A', 'A'})
v = 1x4 cell
    {'A'}    {'A1'}    {'A2'}    {'A3'}

Input Arguments

collapse all

Input text, specified as a string array, character array, or cell array of character vectors.

Excluded variable names, specified as a string array, character array, or cell array of character vectors.

Tips

  • A valid MATLAB variable name consists of letters, digits, and underscores, such that the first character is a letter, and the length of the name is less than or equal to the value returned by the namelengthmax function. Any name that exceeds namelengthmax is truncated in the varname output.

  • If the str input contains any white-space characters, genvarname removes them and capitalizes the next alphabetic character in str. If str contains any nonalphanumeric characters, genvarname translates these characters into their hexadecimal value.

Extended Capabilities

Version History

Introduced before R2006a