Assignment vs Equality

    y = x + 5

In Matlab, the statement shown above is called an assignment statement. It is not an equation as you might be familiar with from your math classes. Assignment statements are used to store the results of calculations. Use an assignment whenever you need to calculate a value that will be used in a later command or operation. Be sure that there is a single variable name on the left hand side or Matlab will correctly report an error.

Assignment Statement

An assignment statement is used to assign a value to a variable name. Once the value has been assigned, the variable name can be used instead of the value. Matlab allows a variable to be assigned to a single scalar value, or an array of values. You will learn more about arrays in later lessons. The = sign is the assignment operator in Matlab. It is used to assign values to variable names. It is not used to represent an equation. In fact, in numeric computing, there is really no such thing as an equation. Note: The equal sign is also used in conditional expressions which will be covered later in this course.

To assign the value 3 to the variable named sideLength:

    sideLength = 3 

Note the lack of a colon ( : ) next to the equal sign, and the lack of a semicolon ( ; ) at the end of the line. These are just a few of the differences between Matlab and Maple. This statement is an assignment statement and does not imply that sideLength equals 3, but rather that the variable named sideLength currently contains the value 3. Reminder: You can use the Workspace window to see the current value of any variables that have been assigned by your work thus far. You can change the value of any variable at any time by executing another assignment statement with the same variable name on the left hand side and the new value on the right hand side.

    sideLength = 7

The semi-colon (;) is used to suppress the output of a particular statement. This is useful when you have trivial set-up or intermediate calculations that will only make the solution harder to read. You will use semi-colons extensively when you define your own functions in a later lesson.

You can display the current value of a variable in the Command Window by typing the variable's name and pressing Enter:

Let's suppose we wanted sideLength to equal 3 again. Another difference between Matlab and Maple is that you can't use your mouse to go to an earlier line on the page and change a command that you've already entered. To repeat a previous command a second time, move the cursor to the prompt >> in the Command Window, and press the up-arrow key. This will scroll through all the commands starting with the most recent. You can also use the Command History window as described in the Overview lesson.

Array Assignments

Matlab allows you to assign several different values to one variable name. For example, you can assign the weights of five different cylinders to a single variable named cylinderWt.

    >> cylinderWt = [ 2.163 3.544 4.191 6.982 7.329 ]

The square brackets ([ ]) are used to group all five values into one array or matrix entity. A single row or column of values is also known as a vector. Next, you will learn how to enter two dimension matrices into variables.

Matrix Assignments

Matlab also allows you to assign a matrix of values to one variable. For example, you can record the temperature at three different places for two different times to a single variable named tempData.

    >> tempData = [ 25.1 25.0 24.3 ; 25.2 24.8 25.3  ]
    
    tempData =

	25.1000   25.0000   24.3000
	25.2000   24.8000   25.3000

    >> 

The square brackets ([ ]) are used to group both rows into one matrix entity. The semi-colon is used to indicate a new row in the matrix. The matrix created above is a 2x3 matrix meaning that it has two rows and three columns. Later, you will learn how to access entries from matrices.