0% found this document useful (0 votes)
17 views5 pages

Lec 4

The document discusses MATLAB variable assignment using the assignment operator (=). Variables can be assigned numeric values, results of expressions, or values of other variables. The assignment operator differs from the equal sign. Variables must follow certain naming rules and cannot use predefined names like keywords. Common predefined variables include pi, eps, inf, ans, i, and j.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Lec 4

The document discusses MATLAB variable assignment using the assignment operator (=). Variables can be assigned numeric values, results of expressions, or values of other variables. The assignment operator differs from the equal sign. Variables must follow certain naming rules and cannot use predefined names like keywords. Common predefined variables include pi, eps, inf, ans, i, and j.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MatLab programming Dr.

Tariq Hussein

The Assignment Operator


In MATLAB the = sign is called the assignment operator. The assignment operator
assigns a value to a variable.

• 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 following shows how the assignment operator works.

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).

The use of previously defined variables to define a new variable is demonstrated


next.

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.

As an example, the last demonstration is repeated below using semicolons.

• 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:

• Once a variable is defined it can be used as an argument in functions. For


example:

Rules About Variable Names


A variable can be named according to the following rules:

• Must begin with a letter.

• Can be up to 63 characters long.

• Can contain letters, digits, and the underscore character.

• Cannot contain punctuation characters (e.g., period, comma, semicolon).

19
MatLab programming Dr. Tariq Hussein

• MATLAB is case-sensitive: it distinguishes between uppercase and lowercase


letters. For example, AA, Aa, aA, and aa are the names of four different variables.

• 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.

Predefined Variables and Keywords

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.)

A number of frequently used variables are already defined when MATLAB is


started. Some of the predefined variables are:

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

inf - Used for infinity.

i – Defined as √−1, which is: 0+ l .0000i.

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.

USEFUL COMMANDS FOR MANAGING VARIABLES

The following are commands that can be used to eliminate variables or to obtain
information about variables that have been created.

21

You might also like