Tools & Toolboxes

On the one hand, Matlab is just a mathematical programming language that can be mainly used to manipulate (preferable huge) matrices and vectors. Yet, on the other hand, it can be extended by adding so-called toolboxes. The big plus of Matlab is that there exists an enormous collection of various httptoolboxes, from an auditory perception toolbox to aircraft control simulation include 3-D graphics.

The toolboxes are either written in the Matlab language itself or can be programmed in C and FORTRAN. Thus (and if you can program in C/FORTRAN) it is relatively straightforward to provide a Matlab function for whatever you are normally do with your computer.

PsychToolbox

The PsychToolbox is such a collection of functions that are often useful for psychological experiments. The core piece are some functions to precisely register key presses, to display images rapidly and in synchrony with the monitor refresh signal, to calibrate the monitor, and a good deal more. In this tutorial we will just concentrate on the keyboard and display functions which will be sufficient for the majority of (non-vision) experiments.

Programming an Experiment

Experiments commonly consists of single trials. Each trial belongs to one condition and in the extreme case there can be as many conditions as trials. But usually a condition is repeatedly presented in several trials. Since Matlab is a mathematical language it is not at all surprising that this information is stored in vectors or matrices.

Suppose, we have 3 conditions and 15 trials per subject (5 repetitions per condition). To just present each of the 3 conditions in ascending order once one would define a condition C variable by

> c = [ 1 2 3 ];

c =

1 2 3

Block Design

The three conditions must be repeated 5 times. Repeating matrices (or vectors) is done by the REPMAT function (repeat matrices). It takes three parameters, the original matrix and the number of repetitions in vertical and horizontal directions. To repeat the row vector C five times, type

> d = repmat(c, 5, 1)

d =

1 2 3

1 2 3

1 2 3

1 2 3

1 2 3

Unfortunately, D is now a 5x3 matrix. But it can be easily converted to a column vector by the (:) operator and the column vector can be then converted to a row vector by the transpose operator '

> e = d(:)'

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3

However, it is much more interesting than printing the condition vector is to visualize it with one of the many plotting commands of Matlab. The easiest is the basic plot command. The second parameter 'o' instructs Matlab just to draw the tiny circles (and no lines).

> plot(e, 'o')

http://www.allpsych.uni-giessen.de/phpwiki/files/plot01.png

To permutate the order of the three blocks, a random order can be generate with the RANDPERM (random permutation) function. RANDPERM takes a number N as the argument and returns a random perumation of the numbers from 1 to N

> c = randperm(3)

c =

3 1 2

The complete condition matrix can again be created with the REPMAT function

> d = repmat(c, 5, 1); e = d(:)'; plot(e, 'o');

http://www.allpsych.uni-giessen.de/phpwiki/files/plot02.png

Randomized Design

If you do not like block design ( they can be quite boring ;) ), you can concatenate the initial condition vector C 5 times by using the third argument of the REPMAT command

> c = [ 1 2 3 ]; d = repmat(c, 1, 5)

d =

1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

> plot(d, 'o')

http://www.allpsych.uni-giessen.de/phpwiki/files/plot03.png

The trial order can be randomized by a trick with the RANDPERM. The vector E contains for each trial from 1 to 15 the condition it belongs to. E(1) for instance contains "1", or E(5) contains "2". RANDPERM(15) returns the numbers from 1 to 15 in a random order

> r = randperm(15)

r =

Columns 1 through 13

10 13 12 3 8 2 5 4 1 15 6 14 9

Columns 14 through 15

7 11

To reorder the conditions in E one can use the permutation vector R. R(1) is 10, thus, if we take the 10th entry D(10) we will have 1. The second entry in R is 13 and E(13) is 1 again. From R(3) = 12, we will get E(12) = 3. Thus, the resulting vector will start with 1 1 3 ...?.

Luckily, it is not necessary to look up each single value by hand. Matlab offers for this purpose a striking simple solution

> f = e(r)

f =

Columns 1 through 13

1 1 3 3 2 2 2 1 1 3 3 2 3

Columns 14 through 15

1 2

And finally we plot then randomized vector again

> plot(f, 'o')

http://www.allpsych.uni-giessen.de/phpwiki/files/plot04.png


Last edited on Friday, June 6, 2003 9:56:58 am.

Valid XHTML 1.0! Valid CSS!
Page Execution took 2.754 seconds