Variable Names
有效的名称
A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB®is case sensitive, soA
anda
arenotthe same variable. The maximum length of a variable name is the value that thenamelengthmax
command returns.
You cannot define variables with the same names as MATLAB keywords, such asif
orend
. For a complete list, run theiskeyword
command.
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 theexist
orwhich
function.exist
returns0
if 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 theclear
function.
Another potential source of name conflicts occurs when you define a function that callsload
oreval
(or similar functions) to add variables to the workspace. In some cases,load
oreval
add variables that have the same names as functions. Unless these variables are in the function workspace before the call toload
oreval
, the MATLAB parser interprets the variable names as function names. For more information, see: