Main Content

Variable Names

有效的名称

A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB®is case sensitive, soAandaarenotthe same variable. The maximum length of a variable name is the value that thenamelengthmaxcommand returns.

You cannot define variables with the same names as MATLAB keywords, such asiforend. For a complete list, run theiskeywordcommand.

Examples of valid names: Examples of invalid names:
x6 6x
lastValue end
n_factorial n!

Conflicts with Function Names

Avoid creating variables with the same name as a function (such asi,j,mode,char,size, andpath). In general, variable names take precedence over function names. If you create a variable that uses the name of a function, you sometimes get unexpected results.

Check whether a proposed name is already in use with theexistorwhichfunction.existreturns0if there are no existing variables, functions, or other artifacts with the proposed name. For example:

existcheckname
ans = 0

If you inadvertently create a variable with a name conflict, remove the variable from memory with theclearfunction.

Another potential source of name conflicts occurs when you define a function that callsloadoreval(or similar functions) to add variables to the workspace. In some cases,loadorevaladd variables that have the same names as functions. Unless these variables are in the function workspace before the call toloadoreval, the MATLAB parser interprets the variable names as function names. For more information, see:

See Also

|||||