Main Content

mustBeValidVariableName

Validate that input name is valid variable name

Since R2020b

    Description

    mustBeValidVariableName(varname) validate that input name is a valid variable name. This function does not return a value.

    mustBeValidVariableName calls the following function to determine if the input is a valid variable name:

    example

    Examples

    collapse all

    Use the mustBeValidVariableName function to ensure a variable name is valid.

    The saveVariable function takes a variable name as input. If the variable name is not a valid name, mustBeValidVariableName throws an error.

    function saveVariable(fname,var)
        arguments
            fname string
            var string {mustBeValidVariableName}
        end
        save(fname,var);
    end
    

    MATLAB® variable names cannot begin with numbers. Attempting to use 8data as a variable causes an error.

    v = "8data";
    saveVariable("myfile",v)
    Error using saveVariable (line 4)
     saveVariable("myfile",v)
                           ^
    Invalid argument at position 2. These are not valid variable names: '8data'.

    Input Arguments

    collapse all

    Variable name, specified as a string scalar, a character vector, or cell array of character vectors.

    Data Types: char | string | cell

    Tips

    • mustBeValidVariableName is designed to be used for property and function argument validation.

    Version History

    Introduced in R2020b