MATLAB Function Reference |
Construct valid variable name from string
Syntax
Description
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).
Note
genvarname returns a string that can be used as a variable name. It does not create a variable in the MATLAB workspace. You cannot, therefore, assign a value to the output of genvarname .
|
Remarks
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.
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.
Example 1
Create four similar variable name strings that do not conflict with each other:
Example 2
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:
Example 3
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:
for k = 1:60 record.(genvarname(['reading' datestr(clock, 'HHMMSS')])) ... = takeReading; pause(60) end
After the program ends, display the recorded data from the workspace:
record record = reading090446: 27.3960 reading090546: 23.4890 reading090646: 21.1140 reading090746: 23.0730 reading090846: 28.5650 . . .
Example 4
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)']) end
As 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.0040
After the program completes, use the who
function to view the workspace variables:
Example 5
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 x
:
Example 6
If you enter a string that is longer than the value returned by the namelengthmax
function, genvarname truncates the resulting variable name string:
namelengthmax ans = 63 vstr = genvarname(sprintf('%s%s', ... 'This name truncates because it contains ', ... 'more than the maximum number of characters')) vstr = ThisNameTruncatesBecauseItContainsMoreThanTheMaximumNumberOfCha
See Also
isvarname
, iskeyword
, isletter
, namelengthmax
, who
, regexp
genpath | get |
© 1994-2005 The MathWorks, Inc.