The Neurophysiological Biomarker Toolbox (NBT)



Getting started with MATLAB

Version: 2016

Approximate time needed to complete the tutorial: 45 min.

Aim of tutorial
MATLAB is a high-level programming language, meaning you can analyze and visualize data without actually knowing much about programming. The NBT is built on MATLAB, but using the NBT toolbox does not require that you know much about MATLAB. Nevertheless, you can achieve more if you do know how MATLAB works. This is why we recommend this tutorial: it will give you an idea of how to use MATLAB and what it can do. Also, at some point you may wish to do an analysis that is not immediately supported by the NBT toolbox. In this case, you may still be able to do it with assistance of your tutors, using the commands below. If you type a command, but do not understand what it does, just ask the tutor.

First, you will be introduced to some MATLAB basics (variables, vectors and matrices). Then you will be introduced to essential MATLAB commands to experience the power of MATLAB in terms of generating data, performing numerical computations and visualizing results. At the same time that you learn basic MATLAB commands, you will get a feel of the relation between amount and variability of data and statistical significance. Understanding these issues is fundamental to design experiments, analyze data and interpret results.

In fact, ethical committees usually ask for a so-called “power analysis” to support that the planned experiments are worthwhile. In other words, how many experiments are required to detect a significant effect given a certain magnitude of this effect and the expected variability in the data? You will learn more about power analysis later in this course.

Before you begin the exercise
Browse the Essential MATLAB Commands to get an impression of how commands are named in MATLAB and which ones you are expected to learn.

MATLAB Basics

To start MATLAB:
Go to 'Start' in Windows ⇒ Programs ⇒ Cursus ⇒ MATLAB ⇒ MATLAB (at the VU University Amsterdam)

You now see the 'command prompt' where you can write commands.
After each MATLAB command, the “return” or “enter” key must be pressed.
Please, type all the expressions in the examples below (instead of doing a copy-paste from the wiki), because MATLAB is sensitive to exact spelling and you will learn that best by making and correcting your mistakes. Just ask if you wonder what’s going on.

Definition of Variables

Variables are assigned numerical values by typing the expression directly, for example:

a = 1+2

a =

     3

MATLAB utilizes the following arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
'^' power operator
' transpose

A variable can be assigned using a formula that utilizes these operators and either numbers or previously defined variables. For example, since a was defined previously, the following expression is valid

b = 2*a;

Note, we have suppressed the display of the content of the variable b in the command window by adding a semicolon at the end. This is handy, because a could also be a vector with hundreds of thousands of values (“samples”) measured with EEG…

To determine the value of a previously defined quantity, type the quantity by itself on the command line and press enter:

b

b =
     6

If your expression does not fit on one line, use an ellipsis (three or more periods at the end of the line), press enter and continue on the next line.

c = 1+2+3+ ...  [press enter]
5+6+7;

Definition of Matrices

MATLAB is based on matrix and vector algebra; even a scalar (single number) is treated as 1-by-1 matrices (often written: 1×1). Therefore, vector and matrix operations are as simple as common calculator operations. Vectors can be defined in two ways.

The first method is used for arbitrary elements:

v = [1 3 5 7];

Creates a 1×4 vector with elements 1, 3, 5 and 7. Note that commas could have been used instead of spaces to separate the elements. Additional elements can be added to the vector:

v(5) = 8;

Yields the vector v = [1 3 5 7 8].

Or elements can be overwritten:

v(1) = 7

v =

     7     3     5     7     8

Previously defined vectors can be used to define a new vector. For example, with v defined above:

a = [9 10];
b = [v a];

Creates the vector b = [7 3 5 7 8 9 10].

You may create vectors with equally spaced elements by indicating the start and end values and the spacing, e.g.,:

t = 0:0.1:10;

Creates a 1×101 vector with the elements 0, 0.1, 0.2, 0.3,…,10.
Note, the middle number defines the increment (like the sample interval in a digitally recorded signal). If only two numbers are given, then the increment is set to a default of 1:

k = 0:10;

creates a 1×11 vector with the elements 0, 1, 2, …, 10.

Matrices are defined by entering the elements row by row:

M = [1 2 4; 3 6 8]     

M =

     1     2     4
     3     6     8

There are a number of special matrices that can be defined:

A null matrix by

 M_empty = [ ];


An 5 x 2 matrix of zeros by

M_zero = zeros(5,2);


An 5 x 2 matrix of ones by

M_ones = ones(5,2);


Here, zeros and ones are so-called build-in MATLAB functions.
The names of the variables/matrices are of course arbitrary.
Try to generate a matrix of zeros with your own choice of m and n.

A particular element of a matrix can be assigned a value:

M(1,2) = 5;


This places the number 5 in the first row, second column.
How would you cross-check that this works? (check with your neighbor for the solution)

You may also read out individual elements:

M(1,3)

ans =

     4

Or rows:

M(2,:)

ans =

     3     6     8

Or columns:

M(:,2)

ans =

     5
     6

Here we read out first the 2nd row of M and then the 2nd column. Is this useful?
Example: If you have a questionnaire with three questions that can be given a score from 1 to 10, and two subjects fill in the questionnaire, then you can arrange your data subjects in rows and questions in columns. In the previous example, M(:,2) would correspond to reading out what the subjects had responded to question 2.
Discuss with your neighbor whether this idea is clear (and ask the tutor if it is not…), because it is essential to understanding how data from many subjects, EEG channels, experimental conditions, or questionnaire scores are conveniently organized. Organized in this way, MATLAB is very powerful for computing basic statistics such as the mean score of specific questionnaire responses as you will soon learn.

Operations and functions that were defined for scalars in the previous section can also be used on vectors and matrices. For example,

a = [1 2 3];
b = [4 5 6];
c = a + b

c =

     5     7     9


Functions are applied element by element. For example:

t = 0:0.1:10;
x = cos(2*t);

Creates a vector x with elements equal to cos(2t) for t = 0, 0.1, 0.2, …, 10.

Operations that need to be performed element-by-element can be accomplished by preceding the operation by a period (”.”).
For example, to obtain a vector x that contains the elements of x(t) = t*cos(t) at specific points in time, you cannot simply multiply the vector t with the vector cos(t). Instead you multiply their elements together:

t = 0:0.1:10;
x = t.*cos(t);

General Information

MATLAB is case sensitive so a and A are two different names.
Commands preceded by a % are not executed. Thus, place % before any comments in scripts. A script is a document with MATLAB commands that is saved with the extension .m.
The command whos gives the names of the variables that have been defined in the workspace:

whos
  Name      	Size                   Bytes  Class

  M         	2x3                    48  double array
  a         	1x3                    24  double array
  ans       	2x1                    16  double array
  b         	1x3                    24  double array
  c         	1x3                    24  double array
  k         	1x11                   88  double array
  t         	1x101                 808  double array
  v         	1x5                    40  double array
  x         	1x101                 808  double array

The command length(x) returns the length of a vector x and size(x) returns the dimensions of the matrix x. Finally, size(x,DIM) returns the size of x along the dimension DIM.

For example:

size(M,1)

ans =

     2

and

size(M,2)

ans =

     3

Thus, the 1st dimension is along the rows and the 2nd dimension is along the columns.
If you forgot whether your subjects are in the rows and your questionnaire data in the columns or vice versa, then this is a quick way to figure out (given that you know that you had two subjects).

Universal question

In case something doesn't work and you start wondering, just ask MATLAB! Type “why” into command line and see for yourself.

.M-files

M-files are macros of MATLAB commands that are stored as ordinary text files with the extension .m (thus: filename.m). An M-file can be either a function with input and output variables or a list of commands. You will learn more about M-files in the following tutorial.

When you are done with this tutorial, please go to the tutorial on Using MATLAB

tutorial/getting_started_with_matlab.txt · Last modified: 2016/05/31 15:44 by Klaus Linkenkaer-Hansen
The NBTwiki platform - version 2.8 - 9 May 2013
Copyright (C) 2008-2015