You are on page 1of 5

1.

6 Identi ers
A variable's name is called an identi er. A valid identi er consists of a sequence of letters (a-z,
A-Z), digits (0-9), or underscores (_), and must start with a letter. Other symbols are not allowed.
Upper and lower case letters differ, meaning MATLAB® is case sensitive. So variables named
balance, BALANCE and Balance represent three different variables. The following are valid
identi ers: c, cat, Cat, n1m1, short1, and hello_there. Note that cat and Cat are different
identi ers. The following are invalid identi ers: 42c (doesn't start with a letter), hi there (has a
space), _tmp (doesn't start with a letter), and cat! (has a symbol other than a letter, digit, or
underscore). Identi ers are also used to name functions and other items in addition to variables;
such items are covered in other sections.

MATLAB has several keywords that cannot be used as programmer-de ned identi ers. The
following table provides a list of keywords. The list of keywords can be found by entering the
command "iskeyword" on the command line.

Table 1.6.1: Keywords in MATLAB.

  break   else   global   return

  case   elseif   if   spmd

  catch   end   otherwise   switch

  classdef   for   parfor   try

  continue     function     persistent     while  

Feedback?

A good practice is to give a variable a meaningful name that describes the variable's purpose,
such as temperature or age rather than just t or A. Abbreviations should only be used if
widely understandable, such as numStudents, but not nmStudents or numStds. Below are
some examples of variable names that are perhaps less meaningful and more meaningful.

Table 1.6.2: Less and more meaningful variable names.

Purpose Less More meaningful names


meaningful
names

ucla
The number of students attending
num numStudentsUCLA
UCLA
nu

sizeTv
The size of a television set szTv
sizeTvDiagonal (if size
measured as its diagonal length size
known to be diagonal)

guess
The number of jelly beans in a jar, numGuessedJellyBeans
num
as guessed by a user userGuessJellyBeans
nJB

Feedback?

When a variable name contains multiple words, two common conventions are to separate each
word with an underscore, as in speed_limit or max_num_people, or to initially capitalize the
words, as in speedLimit or maxNumPeople. Initially capitalizing each word is known as camel
case because the capital letters appear like the humps on a camel. With camel case,
underscores are not used. In some versions of camel case, the rst word is lower case, in other
versions upper case. For naming variables, we will use camel case with the rst word lower case
(e.g. employeeLastName, camelCaseFormatting, thisIsCamelCase).

Programmers typically decide on a particular style convention to keep variable names


consistent within a program. Not following the style convention will not cause an error, but may
make the program harder to read, especially among different members of a programming team.
This material adopts the following style conventions for variable names:

Descriptive variable names


Camel case naming convention, with no underscores

While meaningful names are important, very long variable names, such as
averageAgeOfAUCLAGraduateStudent, can make subsequent statements too long and
thus hard to read. Programmers typically strive to nd a balance. A MATLAB identi er may be
arbitrarily long, however the MATLAB interpreter only pays attention to the rst 63 characters.

Caution: A MATLAB programmer must avoid creating variables with the same name as
keywords, prede ned mathematical constants, or functions. Functions exist internal to MATLAB
(e.g., the function sqrt) or as custom functions created by a programmer (discussed
elsewhere). In general, variable names take precedence over function names. Creating a variable
that uses the name of an existing function, constant, or keyword can produce unexpected
results.
A programmer can check whether a name already exists in the base workspace by using the
statement exist varName where varName is the name to be checked. If the statement
produces the result ans = 1, it means the MATLAB interpreter already recognizes that name
for some purpose, and therefore a different name should be chosen. If a variable with a name
con ict exists, the variable can be easily removed from the base workspace memory by using
the statement clear varName. A good practice is to avoid using variable names that con ict
with MATLAB keywords, prede ned mathematical constants, and MATLAB functions.
A sample of good programming conventions are found in MATLAB Programming Style
Guidelines.

PARTICIPATION
ACTIVITY 1.6.1: MATLAB identi er validator.

Try an identifier: Validate

Awaiting your input...

Feedback?

PARTICIPATION
ACTIVITY 1.6.2: Valid MATLAB variable names.

Which are valid identi ers?

1) r2d2
Correct
Valid
Contains only letters, digits, or underscores, and starts
Invalid with a letter.

2) name$
Correct
Valid
A $-sign is not allowed; only letters, digits, and
Invalid underscores.

3) _2a
Correct
Valid
Must start with a letter.
Invalid

4) pay_day
Correct
Valid
Contains only letters, digits, or underscores, and starts
Invalid with a letter.

5) pay-day
Correct
Valid
Hyphens are not allowed; only letters, digits, and
Invalid underscores.

6) 2a
Correct
Valid
Must start with a letter.
Invalid

7) y____5
Correct
Valid
Ugly, but allowed; there's little reason though to have
Invalid multiple adjacent underscores.

8) switch
Correct
Valid
switch is a keyword and cannot be used as an identi er.
Invalid

Feedback?

How was this section?  


Provide feedback

You might also like