Construct valid variable name from string
Note
genvarname will be removed in a future release. Use matlab.lang.makeValidName and
matlab.lang.makeUniqueStrings
instead.
varname = genvarname(str)
varname = genvarname(str, exclusions)
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.
str can be a string, a string array, a character array, a cell
array of character vectors. 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.
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 string, a string array, a character array, a cell array of character vectors. Specify
the function who in exclusions
to create a variable name that will be unique in the current MATLAB® workspace (see Example 4, below).
Note
genvarname does not create a variable in the MATLAB workspace. You cannot, therefore, assign a value to the output of
genvarname.
Create four similar variable names that do not conflict with each other:
v = genvarname({'A', 'A', 'A', 'A'})
v =
'A' 'A1' 'A2' 'A3'Read a column header hdr from worksheet
trial2 in Excel® spreadsheet myproj_apr23:
[data hdr] = xlsread('myproj_apr23.xls', 'trial2');Make a variable name from the text of the column header that will not conflict with other names:
v = genvarname(['Column ' hdr{1,3}]);Assign data taken from the spreadsheet to the variable in the MATLAB workspace:
eval([v '= data(1:7, 3);']);
Collect readings from an instrument once every minute over the period of an hour
into different fields of a structure. Simulate instrument readings using a random
number. genvarname not only generates unique fieldnames, but also
creates the structure and fields in the MATLAB workspace.
for k = 1:60 record.(genvarname(['reading' datestr(clock, 'HHMMSS')])) = rand(1); pause(60) end
After the program ends, display the recorded data from the workspace:
record
record =
reading092610: 0.6541
reading092710: 0.6892
reading092811: 0.7482
reading092911: 0.4505
reading093011: 0.0838
.
.
.Generate variable names that are unique in the MATLAB workspace by putting the output from the who function in the
exclusions list.
for k = 1:5
t = clock;
pause(uint8(rand * 10));
v = genvarname('time_elapsed', who);
eval([v ' = etime(clock,t)'])
endAs this code runs, you can see that the variables created by
genvarname are unique in the workspace:
time_elapsed =
5.0070
time_elapsed1 =
2.0030
time_elapsed2 =
7.0010
time_elapsed3 =
8.0010
time_elapsed4 =
3.0040After the program completes, use the who function to view the
workspace variables:
who k time_elapsed time_elapsed2 time_elapsed4 t time_elapsed1 time_elapsed3 v
If you try to make a variable name from a MATLAB keyword, genvarname creates a variable name that
capitalizes the keyword and precedes it with the letter x:
v = genvarname('global')
v =
xGlobalIf you enter a character vector that is longer than the value returned by the
namelengthmax function,
genvarname truncates the resulting variable name character
vector:
namelengthmax
ans =
63
vstr = genvarname(sprintf('%s%s', ...
'This name truncates because it contains ', ...
'more than the maximum number of characters'))
vstr =
ThisNameTruncatesBecauseItContainsMoreThanTheMaximumNumberOfChaA valid MATLAB variable name is a character vector of letters, digits, and underscores,
such that the first character is a letter, and the length of the vector is less than or
equal to the value returned by the namelengthmax function. Any character vector 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.
If the str input contains any whitespace characters,
genvarname removes then and capitalizes the next alphabetic
character in str. If str contains any
nonalphanumeric characters, genvarname translates these characters
into their hexadecimal value.