1.4variables and arrays 7
You may have noticed that the result of each of the basic calculations you
performed was always assigned to a variable calledans. Variables are a very
important concept inMATLAB.
1.4 Variables and arrays
A variable is a symbolic name associated with a value. The current value of the
variable is the data actually stored in the variable. Variables are very important
inMATLABbecause they allow us to easily reference complex and changing
data. Variables can reference different data types i. e. scalars, vectors, arrays,
matrices, strings etc.... Variable names must consist of a letter which can be
followed by any number of letters, digits, or underscores.MATLABis case
sensitive i. e. it distinguishes between uppercase and lowercase letters e. g.A
andaare not the same variable.
Variables you have created in the currentMATLABsession can be viewed
in a couple of different ways. The Workspace (shown in Figure 2) lists all the
current variables and allows you to easily inspect their type and size, as well
as quickly plot them. Alternatively, thewhoscommand can be typed in the
Command Window and provides information about the type and size of current
variables. Listing 1.6 shows the output of thewhoscommand after storing and
manipulating a few variables.
Listing 1.6: Using thewhoscommand
1 >> a = 2
2 a =
3 2
4 >> b = 3
5 b =
6 3
7 >> c = a*b
8 c =
9 6
10 >> edinburgh = a+
11 edinburgh =
12 7
13 >> whos
14 Name Size Bytes Class Attributes
16 a 1x1 8 double
17 b 1x1 8 double
18 c 1x1 8 double
19 edinburgh 1x1 8 double
Arrays are lists of numbers or expressions arranged in horizontal rows and
vertical columns. A single row, or single column array is called a vector. An