isvarname
Tests whether text is a valid variable name.
Category: Programming > Functions
Syntax
tf = isvarname(name)
Returns true if name can be used as a variable name.
Description
A valid name starts with a letter and continues with letters, digits and underscores, and is not a reserved keyword such as for or end. Names starting with an underscore or a digit, names with spaces or hyphens, and empty text are all invalid.
Use it before creating a dynamic field name or variable from user text; matlab.lang.makeValidName repairs a name instead of only testing it.
Input arguments
name- Text to test.
Output arguments
tf- Logical scalar.
Example
Valid and invalid names
names = {'speed', 'a_1', '1st', 'my var', 'for', ''};
for k = 1:numel(names)
fprintf('%-6s %d\n', names{k}, isvarname(names{k}));
end
Output
speed 1
a_1 1
1st 0
my var 0
for 0
0