2.2 Creating and Initializing Variables in MATLAB
2.2.1 Initializing Variables in Assignment Statements
The simplest way to initialize a variable is to assign it one or more values in an assignment statement. An assignment statement has the general form
var = expression;
where varis the name of a variable and expressionis a scalar constant, an array, or combination of constants, other variables, and mathematical operations (,, etc.). The value of the expression is calculated using the normal rules of mathematics, and the resulting values are stored in a named variable. The semi- colon at the end of the statement is optional. If the semicolon is absent, the value assigned to varwill be echoed in the Command Window. If it is present, noth- ing will be displayed in the Command Window even though the assignment has occurred.
Simple examples of initializing variables with assignment statements include var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];
The first example creates a scalar variable of type doubleand stores the imag- inary number 40iin it. The second example creates a scalar variable and stores the result of the expression var/5in it. The third example creates a variable and stores a 4-element row vector in it. The third example shows that multiple assignment statements can be placed on a single line, provided that they are sep- arated by semicolons or commas. Note that if any of the variables had already existed when the statements were executed, their old contents would have been lost.
2.2 Creating and Initializing Variables in MATLAB | 25
The last example shows that variables can also be initialized with arrays of data. Such arrays are constructed using brackets ([]) and semicolons. All of the elements of an array are listed in row order. In other words, the values in each row are listed from left to right, with the topmost row first and the bottommost row last. Individual values within a row are separated by blank spaces or commas, and the rows themselves are separated by semicolons or new lines. The following expressions are all legal arrays that can be used to initialize a variable:
[3.4] This expression creates a 1 1 array (a scalar) containing the value 3.4. The brackets are not required in this case.
[1.0 2.0 3.0] This expression creates a 1 3 array containing the row vector [1 2 3].
[1.0; 2.0; 3.0] This expression creates a 3 1 array containing the column vector .
[1, 2, 3; 4, 5, 6] This expression creates a 2 3 array containing the matrix .
[1, 2, 3 This expression creates a 2 3 array containing the matrix 4, 5, 6]
. The end of the first line terminates the first row.
[] This expression creates an empty array, which contains no
rows and no columns. (Note that this is not the same as an array containing zeros.)
c1 2 34 5 6d c1 2 34 5 6d
£ 1 2 3
§
The number of elements in every row of an array must be the same, and the num- ber of elements in every column must be the same. An expression such as
[1 2 3; 4 5];
is illegal because row 1 has three elements while row 2 has only two elements.
Programming Pitfalls:
The number of elements in every row of an array must be the same, and the number of elements in every column must be the same. Attempts to define an array with different numbers of elements in its rows or different numbers of elements in its columns will produce an error when the statement is executed.
The expressions used to initialize arrays can include algebraic operations and all or portions of previously defined arrays. For example, the assignment statements
a = [0 1+7];
b = [a(2) 7 a];
will define an array a [0 8] and an array b[8 7 0 8].
Also, not all of the elements in an array must be defined when it is created.
If a specific array element is defined and one or more of the elements before it are not, then the earlier elements will automatically be created and initialized to zero. For example, if cis not previously defined, the statement
c(2,3) = 5;
will produce the matrix . Similarly, an array can be extended by specifying a value for an element beyond the currently defined size. For example, suppose that array d[1 2]. Then the statement
d(4) = 4;
will produce the array d[1 2 0 4].
The semicolon at the end of each assignment statement shown previously has a special purpose: it suppresses the automatic echoing of valuesthat normally occurs whenever an expression is evaluated in an assignment statement. If an assignment statement is typed without the semicolon, the result of the statement is automatically displayed in the Command Window as follows:
ằ e = [1, 2, 3; 4, 5, 6]
e =
1 2 3 4 5 6
If a semicolon is added at the end of the statement, the echoing disappears.
Echoing is an excellent way to quickly check your work, but it seriously slows down the execution of MATLAB programs. For that reason, we normally suppress echoing at all times by ending each line with a semicolon.
However, echoing the results of calculations makes a great quick-and-dirty debugging tool. If you are not certain what the results of a specific assignment statement are, just leave off the semicolon from that statement, and the results will be displayed in the Command Window as the statement is executed.
✷ Good Programming Practice:
Use a semicolon at the end of all MATLAB assignment statements to suppress echoing of assigned values in the Command Window. This greatly speeds program execution.
c5 c00 00 05d
2.2 Creating and Initializing Variables in MATLAB | 27
✷ Good Programming Practice:
If you need to examine the results of a statement during program debugging, you may remove the semicolon from that statement only so that its results are echoed in the Command Window.