|MATLAB Function Reference|
Construct valid variable name from string
varname = genvarname(str)
constructs a string
varname that is similar to or the same as the
str input, and can be used as a valid variable name.
str can be a single character array or a cell array of strings. If
str is a cell array of strings,
genvarname returns a cell array of strings in
varname. The strings in a cell array returned by
genvarname are guaranteed to be different from each other.
varname = genvarname(str, exclusions)
returns a valid variable name that is different from any name listed in the
exclusions input. The
exclusions input can be a single character array or a cell array of strings. Specify the function
who in the
exclusions character array to create a variable name that will be unique in the current MATLAB workapace (see Example 4, below).
A valid MATLAB variable name is a character string of letters, digits, and underscores, such that the first character is a letter, and the length of the string is less than or equal to the value returned by the
namelengthmax function. Any string that exceeds
namelengthmax is truncated in the
varname output. See Example 6, below.
The variable name returned by
genvarname is not guaranteed to be different from other variable names currently in the MATLAB workspace unless you use the
exclusions input in the manner shown in Example 4, below.
If you use
genvarname to generate a field name for a structure, MATLAB does create a variable for the structure and field in the MATLAB workspace. See Example 3, below.
str input contains any whitespace characters,
genvarname removes then and capitalizes the next alphabetic character in
str contains any nonalphanumeric characters,
genvarname translates these characters into their hexadecimal value.
Create four similar variable name strings that do not conflict with each other:
Read a column header
hdr from worksheet
trial2 in Excel spreadsheet myproj_apr23:
Make a variable name from the text of the column header that will not conflict with other names:
Assign data taken from the spreadsheet to the variable in the MATLAB workspace:
Collect readings from an instrument once every minute over the period of an hour into different fields of a structure.
genvarname not only generates unique fieldname strings, but also creates the structure and fields in the MATLAB workspace:
After the program ends, display the recorded data from the workspace:
Generate variable names that are unique in the MATLAB workspace by putting the output from the
who function in the
As this code runs, you can see that the variables created by
genvarname are unique in the workspace:
After the program completes, use the
who function to view the workspace variables:
If you try to make a variable name from a MATLAB keyword,
genvarname creates a variable name string that capitalizes the keyword and precedes it with the letter
If you enter a string that is longer than the value returned by the
namelengthmax function, genvarname truncates the resulting variable name string:
© 1994-2005 The MathWorks, Inc.