genvarname

Construct valid variable name from string

Syntax

varname = genvarname(str)
varname = genvarname(str, exclusions)

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® workspace (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.

Examples

Example 1

Create four similar variable name strings that do not conflict with each other:

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

Example 2

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);']);

Example 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 fieldname strings, 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
         .
         .
         .

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:

who

k           time_elapsed   time_elapsed2  time_elapsed4
t           time_elapsed1  time_elapsed3  v

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:

v = genvarname('global')
v =
   xGlobal

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

More About

expand all

Tips

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.

Was this topic helpful?