Programming Previous page   Next Page

Variables

This section covers the following topics:

Rules for Variable Names

Although variable names can be of any length, MATLAB uses only the first N characters of the name, (where N is the number returned by the function namelengthmax), and ignores the rest. Hence, it is important to make each variable name unique in the first N characters to enable MATLAB to distinguish variables. Also note that variable names are case sensitive.

For more information: See Naming Variables in the MATLAB Programming documentation.

Making Sure Variable Names Are Valid

Before using a new variable name, you can check to see if it is valid with the isvarname function. Note that isvarname does not consider names longer than namelengthmax characters to be valid.

For example, the following name cannot be used for a variable since it begins with a number.

For more information: See Naming Variables in the MATLAB Programming documentation.

Don't Use Function Names for Variables

When naming a variable, make sure you are not using a name that is already used as a function name. If you do define a variable with a function name, you won't be able to call that function until you clear the variable from memory. (If it's a MATLAB built-in function, then you will still be able to call that function but you must do so using builtin.)

To test whether a proposed variable name is already used as a function name, use

Checking for Reserved Keywords

MATLAB reserves certain keywords for its own use and does not allow you to override them. Attempts to use these words may result in any one of a number of error messages, some of which are shown here:

Use the iskeyword function with no input arguments to list all reserved words.

Avoid Using i and j for Variables

MATLAB uses the characters i and j to represent imaginary units. Avoid using i and j for variable names if you intend to use them in complex arithmetic.

If you want to create a complex number without using i and j, you can use the complex function.

Avoid Overwriting Variables in Scripts

MATLAB scripts store their variables in a workspace that is shared with the caller of the script. When called from the command line, they share the base workspace. When called from a function, they share that function's workspace. If you run a script that alters a variable that already exists in the caller's workspace, that variable is overwritten by the script.

For more information: See Scripts in the MATLAB Programming documentation.

Persistent Variables

To get the equivalent of a static variable in MATLAB, use persistent. When you declare a variable to be persistent within a function, its value is retained in memory between calls to that function. Unlike global variables, persistent variables are known only to the function in which they are declared.

For more information: See Persistent Variables in the MATLAB Programming documentation.

Protecting Persistent Variables

You can inadvertently clear persistent variables from memory by either modifying the function in which the variables are defined, or by clearing the function with one of the following commands:

Locking the M-file in memory with mlock prevents any persistent variables defined in the file from being reinitialized.

Global Variables

Use global variables sparingly. The global workspace is shared by all of your functions and also by your interactive MATLAB session. The more global variables you use, the greater the chances of unintentionally reusing a variable name, thus leaving yourself open to having those variables change in value unexpectedly. This can be a difficult bug to track down.

For more information: See Global Variables in the MATLAB Programming documentation.


Previous page  Debugging Strings Next page

© 1994-2005 The MathWorks, Inc.