Assignment
From CDS 130
1. Objectives
 To introduce the notion of assignment and how it corresponds to a bit pattern
 To distinguish between assignment and equality
 To introduce the clear command
 To introduce the meaning of the semicolon at the end of a line
 To introduce the comment character
 To understand how to form valid variable names
2. Motivation
 One of the most frequent operations in programming is assignment.
3. Priming questions
 Your algebra instructor writes
x = x + 1
on the board and asks you what you can conclude. What is your answer?
4. Notes
4.1. Definition
 In programming, assignment is associating a value to a variable.
 For example, in a program, you may assign the number
1.999999999999
to the variable namedx
. Later in the program, you may assign the number3.14159
to the variable namedx
. At any point in the program, you may ask "what is the last value that was assigned tox
?" or say "take the current value assigned tox
and double it".
4.2. Assignment
In a spreadsheet we enter a number 1.0
into cell A1
. We have assigned the value of 1.0
to A1
.
In MATLAB the equal sign is used to indicate assignment. The statement
A1 = 1.0
assigns the value of 1.0
to variable named A1
. When you assign a decimal number to a variable, MATLAB writes the bit pattern associated with the decimal number into memory.
4.3. Connection to bit patterns
If you entered A1 = 1 B1 = 2 A1 + B1 
the last line is telling MATLAB to do the following binary addition 0 0 0 1 0 0 1 0 + _______ 
Note 

The above is an approximation of what actually happens behindthescenes. In reality, MATLAB writes a bit pattern with 64 values to represent the number 1. Also, the bit pattern for the decimal number 
4.4. Assignment versus Equal
 In MATLAB, the equal character is used for assignment.
 In a MATLAB program the equal character does not mean equal in the sense that you are used to (the mathematical sense). That is,
 Some languages attempt to avoid this confusion by using a different symbol for assignment; for example, instead of
A1 = 1.0
, the expressionA1 := 1.0
orA1 < 1.0
is used.
4.5. Assignment versus Equal
The reason these languages use a different symbol for assignment is to avoid confusion. If you were thinking in the mathematical sense and saw this in a computer program
A1 = A1 + 1.0
you might rewrite this equation as
A1  A1 = 1.0
and then
0 = 1.0
which does not make sense. If A1
had been previously been assigned a value, then
A1 = A1 + 1.0
is a valid programming statement. It reads "Assign to A1 the value of whatever A1 is now plus 1.0." Said another way, the above statement reassigns the value of A1
.
4.6. Whitespace around equal sign
 Whitespace is optional around the equal character
A1=1 A1 = 1 A1 =1
all mean the same thing. Some programmers use the convention of connecting the equal sign to the variable, as in
A= 1
to emphasize that A
is being assigned to the value of 1
.
4.7. The endofline semicolon
 If a line does not have a semicolon at the end, MATLAB assumes that you want a value of a variable to be displayed.
If you enter
A1 = 20.0;
MATLAB assigns the value of 20.0 to a variable named A1
.
If you had entered
A1 = 20.0
MATLAB will assign the value of 20.0 to a variable named A1
and then display the value of A1
:
A1 = 20
4.8. The endofline semicolon continued
You can also display the value of a variable after it has been assigned. For example, you could enter
A1 = 20; B1 = 20; A1
and MATLAB will display
A1 = 20
If you had ommited the semicolon on the first line, you would see
A1 = 20 A1 = 20
4.9. Assignment Example
Do the following in MATLAB syntax:
What is B1 after the instructions are executed? 

Could you do the above in Excel?
Answer 

First you would enter 
4.10. Assignment Example
Write a MATLAB program that does the following


4.11. Basic Syntax Rules
Thus far, we have assigned values to variables named A1, B1
, etc. following the way cells are named in a spreadsheet. MATLAB allows other variable names besides a capital letter followed by an integer. However, in the same way that the hospital won't let you name your baby "l33t
", there are rules for variable names.
As a general rule:
 Use only characters that are letters, numbers, or the underscore. For example,
A1, Bob, bob, cat3, tag3, my_variable
.  Don't start a variable name with a number. For example,
1A
or333BB
.
4.12. General Syntax Rules
 A variable may not start with a number or underscore (
3A
and_A
are not allowed).  Variable names should be shorter than 64 characters.
 Lowercase and uppercase variables are different (
B1
is different fromb1
).
According to [1],
A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLABĀ® is case sensitive, so A and a are not the same variable. The maximum length of a variable name is the value that the namelengthmax command returns.
4.13. General Syntax Recommendations
 Some variable names are not recommended:
if, pi, for, end, matrix, array
. To determine if a variable name is "not recommended", typewhich NAME
on the command line. If MATLAB says'NAME' not found
, thenNAME
is a safe name to use.
4.14. Syntax Rules Example
Which of the following are valid names? Which are not recommended?


4.15. The comment character
 A comment character instructs the interpreter to ignore everything (the comments) that follows it.
 Most programming languages have a comment character (some use multiple characters to represent a comment, e.g.,
//
).  MATLAB's comment character is the percent sign:
%
.  Comments are added to make programs easier for a human to understand.
For example, the program
A1 = 20.0; B1 = A1 + 13.0; B1 = B1 + 12.0; B1
could have been written as
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Start of program to demonstrate assignment A1 = 20.0; % Assign the value of 20 to a variable named A1 B1 = A1 + 13.0; % Assign the value of A1 plus 13.0 to a variable named B1 B1 = B1 + 12.0; % Assign the previous value of B1 plus 12.0 to the variable named B1 B1 % Display the value of B1 % End of program to demonstrate assignment %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4.16. The "clear" command
When you start MATLAB, all of the variables that you have previously assigned values to no longer exist. Said another way, the values and the associated names are cleared from memory. Sometimes you will want to remove a variable (most often an array) from memory without exiting and restarting MATLAB. To see how the clear command works, note that if you enter
A1 = 10; clear A1; b = A1 + 1.0
you will see an error message. On the second line you have instructed MATLAB to remove the variable A1
from memory. On the last line, you have asked MATLAB to add 1.0 to the value of a variable that does not have a value associated with it. To clear all variables, type clear
or clear all
.
5. Summary Video
6. Questions
6.1. Syntax I
What will happen when the following commands are entered (take into account semicolons)?
A = 1; B = 2 C = A + B
Answer 


A = 1; B = A+1; B = B*2
Answer 


a = 2 b = 4 c = a + b
Answer 

The first two lines result in 
6.2. Syntax II
Each of the following has one error in it that will prevent the code from running if typed on the command line. What is the error?
clear; a = b;
Answer 

Answer: The clear statement deletes all variables. The second line tells MATLAB to assign the value of 
clear; b = 1; c = a+b;
Answer 

The clear statement removes all variables from memory. The third line is attempting to assign a value using an undefined variable (a was not defined). 
clear; A1 = 10; 1A = A1;
Answer 

A variable name may not start with a number. 
clear; A = 1; B = A+2; C = B+2; D = D+2;
Answer 

In the last line, 
clear; A = 1; B = 2; A+B = 3;
Answer 

A variable name may not have a plus sign (or, only a variable may be assigned a value and the last line is attempting to assign a value to the result of an addition). 
6.3. Valid Names
Which of the following are valid names? Which are not recommended?

plot

matrix

_twelve

seven

My Variable

MyVariable

A1^3

case