Matlab Tutorial #1: Navigation & Matrices

Note

Topics covered: Directories, navigation, matrices, strings, integers

Commands used: pwd, cd, ls

Overview of the Matlab Interface

When you first open Matlab, you will notice that it has four distinct sections: Three windows, and a ribbon of buttons at the top, similar to the layout of Microsoft Word. The window on the left is the Navigation window, the central window is the Command Window, and the upper-right window is the Workspace. The Navigation window contains a list of all the folders located within the folder you are currently in; for example, if I am in my home directory, you may see sub-folders such as Documents, Desktop, Music, and so on.

The Command Window is where you can directly type commands and see the output of those commands. As you will see below, these can be commands for navigating to different directories, listing the contents of the current directory, or creating variables that can be used in scripts. Most of what we will be doing in this tutorial requires you to use the Command Window. The Workspace window contains variables that you have created in your current session. The concept of variables is discussed below in the section on matrices.

../_images/01_Matlab_Layout.png

Introduction to Matrices

In addition to being able to navigate from the command line and the ability to host software programs such as SPM12 and the CONN toolbox, Matlab allows the user to create and manipulate matrices. For example, we can create a matrix simply by typing:

x = 1

Once you press enter, this will automatically print output to the terminal that says “x = 1”. Note that “x” is now stored as a variable in the Workspace window; you can see both the name of the variable, and its value. The reason we call these objects “variables” is that they can contain any value, and can be updated in a command structure such as a for-loop, which will be discussed in a later chapter.

If you ever want to find out the value contained within the variable, you can simply type x at the command line and press enter, or you can look in the Workspace. For even more detailed information about the variable, you can type:

whos x

This will display the following:

Name      Size            Bytes  Class     Attributes
x         1x1                 8  double

For now we will ignore the last three fields, and focus on the first two. The first column, “Name”, is simply the name of the variable; but notice that the second column, “Size”, contains the value “1x1”. Even a single value stored in a variable - in this case, the value “1” contained in the variable “x” - is labeled by Matlab as a 1x1 matrix. If we wanted to create a variable that spanned multiple dimensions, such as a 4x5 matrix, we could type something like the following:

a = [4 10 1 6 2; 8 1.2 9 4 25; 7.2 5 7 1 11; 0 0.5 4 5 56; 23 83 13 0 10];

Note

In this line of code, we ended it with a semicolon (;). This suppresses the output from being automatically printed to the terminal, while still storing the value in the variable.

Notice that we now have a new variable in our Workspace window called “a”, which is a 4x5 matrix. If you type a in the terminal and press enter, you will see the following:

 4.0000   10.0000    1.0000    6.0000    2.0000
 8.0000    1.2000    9.0000    4.0000   25.0000
 7.2000    5.0000    7.0000    1.0000   11.0000
      0    0.5000    4.0000    5.0000   56.0000
23.0000   83.0000   13.0000         0   10.0000

Immediately after the variable name, if you type a number or pair of numbers in parentheses, it will index the rows and columns of this matrix - in other words, extract certain parts of it. For example, typing a(1) will return 4; typing a(9) will return 0.5. Two-dimensional matrices have the following index structure, in which the first cell in the upper-left is indexed as 1, the cell below that is indexed as 2, and so on until the first cell of the next column, which in this case would be indexed as 6.

../_images/01_Matlab_Indexing.gif

Instead of using a single number for indexing, we could use a pair of numbers separated by commas. The first number of the pair indexes the row of the matrix, and the second number indexes the column. For example, if we wanted to extract the value contained in the third row and fourth column of the matrix, we could type:

a(3,4)

Which would return a value of 1. This indexing can be extended to three- and four-dimensional matrices, as well as higher-dimensional ones, which will not be covered here.

An entire column or row can be extracted by using a colon (:). For example, if we wanted to extract the second column of the matrix a, we could type:

a(:,2)

Likewise, to extract the fifth row, we could type:

a(5,:)

You can also extract every nth element of the matrix by inserting another number in-between the first and last indices. For example,

a(10:2:20)

Will start at the matrix index 10 and go up through index 20, increasing by intervals of 2. In this case, the matrix values contained within indices 10, 12, 14, 16, 18, and 20 will be extracted.

This will take some time to get used to, but with practice, you will become more fluent with how to index rows and columns.

Matrix Arithmetic

Matrices can also be added to and subtracted from each other, multiplied and divided by one another. For example, if we create two matrices:

a = [9 8 7; 3 2 1]
b = [1 2; 4 5 ; 7 8]

We can multiply them by typing c = a*b. (Note that the inner dimensions must be identical; in this case, a is a 2x3 matrix, and b is a 3x2 matrix. For more information about matrix multiplication, click here).

a'.*b

Will transpose a to be a 3x2 matrix. The resulting product will also be a 3x2 matrix. You can also add or subtract matrices, as long as they have the same dimensions. Multiplying by a constant requires no special transformations, and will simply multiply each cell of the matrix by a single number:

a'-b
a'+b
7*a

Concatenating Matrices and Strings

We can juxtapose, or concatenate, two or more matrices by using brackets. For example,

d = [a' b]

Which will simply place the two matrices side by side, in this case creating a new 3x4 matrix. However, we can also use brackets to concatenate both numbers and text, or strings. Later, we will see how this can be used with scripting to automate analyses. For now, create a new variable:

x = 'subject-'
y = 1

We could try to concatenate them into a new variable, z, by using brackets:

z = [x y]

You may be surprised to see that the output from this command is simply subject-. It turns out that we will need both the variables x and y to be strings, which we could do by typing y='1'. If we want to keep the option to use y as either an integer or a string, we can use the command num2str to convert the variable when we need to:

z = [x num2str(y)]

Which will generate the expected output, subject-1. We will revisit this in a later chapter on scripting, in which we will need to loop over multiple subjects and analyze them.

Video

Click here to see a video overview of the commands cd, ls, and pwd - the basic commands you will need to navigate around your directory tree. This particular video is written using a Unix terminal, but the basic navigation commands are the same between Unix and Matlab.


Exercises

Open a Matlab terminal, and do the following exercises:

  1. Type ls ~ and note what it returns; then type ls ~/Desktop. How are the outputs different? Why?

  1. Navigate to the Desktop by typing cd ~/Desktop. Type pwd and note what the path is. Then create a new directory using the mkdir command, choosing a name for the directory on your own, such as mkdir myFolder. Navigate into that new directory and think about how your current path has been updated. Does that match what you see from typing pwd from your new directory? Take a screenshot of the Matlab terminal that shows the pwd command being typed from the newly created directory, and the output from that command.

  2. Define the terms cd, ls, and pwd in your own words.

  1. Using the a matrix defined above, what would a(14) return? Make your prediction about the output before typing the code and pressing return.

  2. Use the help function to find out what the keyword end does by typing help end (for our purposes, read the third paragraph of the output from the help file). Use this keyword with the a matrix defined above to print rows 2 through 5. Do the same procedure to print only the last four rows of columns 3 through 5.