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”):
Go to the directory H using cd.
Make a directory “EEG_course” using mkdir:
and make your own directory:
Alternatively, first define the string your_directory. For example:
your_directory = 'einstein'
Go to your directory and check that you are there.
If you have just completed the previous tutorial, please clean your workspace:
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:
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'.
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.
Plot numbers with connecting lines (the default).
Plot numbers with and without connecting lines.
Change the symbol to red circles.
Try to plot both the red circles and the connecting lines on the plot. Hint, type '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'.
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…