Lec 4
Lec 4
Tariq Hussein
• The left-hand side of the assignment operator can include only one variable name.
The right-hand side can be a number, or a computable expression that can include
numbers and/or variables that were previously assigned numerical values.
The last statement (x = 3x -12) illustrates the difference between the assignment
operator and the equal sign. If in this statement the = sign meant equal, the value of
x would be 6 (solving the equation for x).
17
MatLab programming Dr. Tariq Hussein
• If a semicolon is typed at the end of the command, then when the Enter key is
pressed, MATLAB does not display the variable with its assigned value (the variable
still exists and is stored in memory).
• If a variable already exists, typing the variable's name and pressing the Enter key
will display the variable and its value in the next two lines.
• Several assignments can be typed in the same line. The assignments must be
separated with a comma (spaces can be added after the comma). When the Enter key
is pressed, the assignments are executed from left to right and the variables and their
assignments are displayed. A variable is not displayed if a semicolon is typed instead
of a comma. For example, the assignments of the variables a, B, and C above can all
be done in the same line.
18
MatLab programming Dr. Tariq Hussein
• A variable that already exists can be reassigned a new value. For example:
19
MatLab programming Dr. Tariq Hussein
• No spaces are allowed between characters (use the underscore where a space is
desired).
• Avoid using the name of a built-in function for a variable (i.e., avoid using cos, sin,
exp, sqrt, etc.). Once a function name is used to for a variable name, the function
cannot be used.
There are 20 words, called keywords, that are reserved by MATLAB for various
purposes and cannot be used as variable names. These words are:
When typed, these words appear in blue. An error message is displayed if the user
tries to use a keyword as a variable name. (The keywords can be displayed by typing
the command iskeyword.)
ans - A variable that has the value of the last expression that was not assigned to a
specific variable (see Tutoriall-1). If the user does not assign the value of an
expression to a variable, MATLAB automatically stores the result in ans.
pi - The number π.
eps - The smallest difference between two numbers. Equal to 2^(-52), which is
approximately 2.2204e-016.
20
MatLab programming Dr. Tariq Hussein
j - Same as i.
NaN - Stands for Not-a-Number. Used when MATLAB cannot determine a valid
numeric value. Example: 0/0.
The predefined variables can be redefined to have any other value. The variables pi,
eps, and inf, are usually not redefined since they are frequently used in many
applications. Other predefined variables, such as i and j , are sometime redefined
(commonly in association with loops) when complex numbers are not involved in
the application.
The following are commands that can be used to eliminate variables or to obtain
information about variables that have been created.
21