mustBeValidVariableName

Checks that text is a valid MATLAB variable name.

Category: Programming > Functions

Syntax

mustBeValidVariableName(A)

Checks that A holds valid variable names. Returns nothing if the check passes and throws an error if not.

Description

mustBeValidVariableName is a validator: it inspects its input and either returns silently or raises an error with the identifier MATLAB:validators:mustBeValidVariableName. The error message states the requirement that was violated, so the caller sees what was expected rather than a generic failure.

A valid name starts with a letter, continues with letters, digits or underscores, is not a reserved keyword, and is no longer than namelengthmax. A may be a cell array or string array of names; the error lists the invalid ones.

Input arguments

A
The value to check. Any array of the class the validator accepts.

Example

Passing and failing input

mustBeValidVariableName('myVar');
disp('passed')
try
    mustBeValidVariableName('1a')
catch err
    disp(err.message)
end
Output
passed
The following are not valid variable names: '1a'.

See also

isvarname matlab.lang.makeValidName iskeyword mustBeText