Why donate?

- Tutorials, follow the NBT course

**Follow us **

Version: 2016

Approximate time needed to complete the tutorial: 20 min.

**Aim of tutorial**

To understand the concept of build-in functions from MATLAB and to learn how to use them.

Start MATLAB and find your current working directory using **pwd** (“print working directory”):

pwd

Go to the directory **H** using **cd**.

cd H:\

Make a directory “EEG_course” using **mkdir**:

mkdir('EEG_course')

and make your own directory:

mkdir('your_directory')

Alternatively, first define the string **your_directory**. For example:

your_directory = 'einstein' mkdir(your_directory)

Go to your directory and check that you are there.

cd your_directory pwd

If you have just completed the previous tutorial, please clean your workspace:

clear all

In a typical investigation you have two sets of data of **n** measurements, e.g., from **n** subjects or mice. You have two sets because you want to compare two subject groups or two different conditions. Define the number of samples **n** you want to work with, e.g., n = 25.

n = 25;

Generate a vector **A** of **n** random numbers using the **randn** function. Random numbers from the **randn(m,n)** function are drawn from a Normal_distribution with the mean value ('mu') of 0 and the standard deviation ('sigma') of 1. Thus, for a so-called row vector with elements placed in **n** columns, m=1 and n=25.

Let us assume that each of the **n** numbers represent the peak frequency of alpha oscillations (8–13 Hz) as determined in **n** experiments. We then have to change the mean and standard deviation of the distribution:

mu = 10 sigma = 0.5 m = 1 A = mu + randn(m,n)*sigma

Oops, now you see a lot of numbers in the command window…!?

To suppress the display of the content of the variable **A** in the command window, add a semicolon at the end:

A = mu + randn(m,n)*sigma;

You may assess any element(s) in vector **A** by listing their index/indices. For example:

A(3) ans = 9.2031

or

A(3:7) ans = 9.2031 9.2795 10.2856 9.8001 10.3450

Thus, showing the elements 3 through 7 of vector **A**.

Actually, you are unlikely to have exactly the numbers listed above in your workspace. **Discuss with your neighbor why this is so!**

We could also have defined 'A' using: A = 10 + randn(1,25)*0.5

*However, it is strongly recommended to NOT work directly with numbers* (especially in programs or so-called scripts as you will learn later) because:

1) You cannot easily overview, e.g., what input argument '10' refers to, whereas 'mu' is likely to remind you.

2) In long scripts, where the same input variable is used in several places, you inevitably will forget to change the value everywhere with confusing/wrong results.

Input variables are better defined in the beginning of a script. You can always check the content/value of a variable by writing the variable name on the command line and pressing Enter. Or check that it has the right size using 'size(variable)'.

The term “workspace” refers to all the variables currently available for viewing, manipulating, saving or clearing. To view the content of your workspace use the command 'whos'.

whos

MATLAB is good at visualizing variables, like the numbers in 'A'.

Type 'help plot' to see one option.

Note, it is often sufficient to read the first 3–5 sentences of the help description to understand how to use a MATLAB function or to solve your problem when receiving an error message.

help plot

Plot numbers with connecting lines (the default).

plot(A)

Plot numbers with and without connecting lines.

plot(A,'.')

Change the symbol to red circles.

plot(A,'ro')

Try to plot both the red circles and the connecting lines on the plot. Hint, type 'help hold'. NOTE, funtions in Matlab help are always written in capital letters; however, when using them in Matlab, you should write them in lowercase letters (don't ask me about the logic…).

help hold

Compute the mean and standard deviation of the numbers in 'A' using 'mean' and 'std' and put the result in two new variables, e.g., 'A_mean' and 'A_std'.

*MATLAB commands, such as 'mean' and 'std' should not be used as variable names!*

However, try to first guess the result! (cf. the definition of 'A')…

A_mean = mean(A) A_std = std(A)

A_mean and A_std are now two variables (numbers) containing the mean and standard deviation of the 'n' numbers in 'A'.

**Did you get the expected value?
If not, why?
Any suggestions for how the mean and standard deviation could come closer to the expected value?
How can you quickly test your suggestion?
**

Hint: type **n** and use the “arrow-up” key to quickly redefine the size of 'n', type **A** (i.e., **A** and the “space key”) and BEFORE pressing “enter” press the “arrow-up” key. This should immediately retrieve the command where you generated ‘A’ and allows you to quickly generate a new 'A'.

Finally, re-compute 'A_mean'. Did you get closer to the expected value?

The 'plot' command is not particularly useful for understanding the link between sample size ('n') and the accuracy of the estimate of the mean or standard deviation of a distribution. We will therefore calculate a histogram of 'A' using 'hist'.

hist(A)

This is a rather crude histogram. In the help description of 'hist' you will find that using [N,X] = HIST(Y,X) will allow you to define a more detailed binning vector 'X' and output the distribution of 'Y' in a variable 'N'. Note, I have taken the variable symbols from the help menu of MATLAB, but usually you would want to change these symbols (!).

Let’s define a vector ‘A_bin’ with smaller bin widths than the default value 1:

A_bin = [7:0.5:13];

Note, this is the efficient way to define a vector in MATLAB, which could also have been defined by typing…

A_bin = [7 7.5 8 8.5 9 9.5 10 10.5 11 11.5 12 12.5 13];

We now compute the distribution of 'A' into the bins of 'A_bin':

[A_hist,A_bin] = hist(A,A_bin);

'A_hist' is a vector that now contains the number of numbers belonging to each of the intervals defined in 'A_bin'. Plot the resulting distribution in a new figure window, using 'figure'.

figure plot(A_bin,A_hist)

Alternatively, you may plot a bar diagram using 'bar', which was the default plot in 'hist' (This you may actually verify by displaying the build-in MATLAB script by typing 'type hist'.

figure bar(A_bin,A_hist)

You may want to test different widths of the binning vector until the distribution looks “smooth”.

**You are now ready for the tutorial on Variability and statistics exercise**

If you have time, however, you may also explore the **Getting started with matlab** videos below (for which you need headphones)

and the ** Working in the Matlab Development Environment**