Day 1 of Psychtoolbox practical course is dedicated to MATLAB functions important for building experimental paradigms
The code file can be downloaded:
https://drive.google.com/file/d/0B7HyuFpj0ptpcXF4ZzcwaWpDRUU/view?usp=sharing
Psychtoolbox (PTB) practical course by Volodymyr B. Bogdanov, Kyiv 2017, Day 1
1. Psychtoolbox (PTB)
practical course
by Volodymyr B. Bogdanov
vlabogd@yahoo.com
Kyiv 2017
Day 1:
MATLAB functions important for
building experimental paradigms
2. Psychophysics Toolbox Version 3 (PTB-3) is a free
set of MATLAB and GNU Octave functions for
vision and neuroscience research.
It makes it easy to synthesize and show accurately
controlled visual and auditory stimuli and interact
with the observer.
http://psychtoolbox.org/
https://www.gnu.org/software/octave/https://mathworks.com/
3. Why MATLAB?
Because it is one of the most popular,
powerful and flexible data analysis software.
Richard Matthew Stallman
Why GNU Octave?
Because it rather similar to
MATLAB and free!
https://youtu.be/ZPPikY3uLIQ
Specifically, free software means users have
the four essential freedoms: (0) to run the
program, (1) to study and change the program
in source code form, (2) to redistribute exact
copies, and (3) to distribute modified versions.
The GNU Project is a free-software,
mass-collaboration project, first
announced on September 27, 1983 by
Richard Stallman at MIT.
Motivation
This is exactly what you can do with PTB:
you run, study, modify and redistribute
scripts chic can be used for experimental
studies in neuroscience.
Therefore during this course we will use
both Matlab and GNU Octave to learn PTB.
5. MATLAB Graphical User Interface (GUI)
Command window
Workspace
Command history
Current folder
6. GNU Octave Graphical User Interface (GUI)
Command windowWorkspace
Command history
Current folder
7. Files formats:
*.m – script files (programs, functions) – text files that contain code
*.mat – data files (any kind of variables stored)
8. Data operations in MATLAB or GNU Octave
Elementary operations
Concatenation
Arithmetic operations
Logical operations
Matrix indexing
Functions
Control (flow) statements
Visualization
Saving, clearing and loading data
9. [] matrix
OperatorsData classes
Elementary operations
‘’ string
{} cell
a.b structure
MATSAB syntax:
logical
comment is formed with a percent
sign :
% this is a comment, not a command
= equal sign assigns a value to a
variable
a = 7
MATLAB is case-sensitive!
. dot separates decimals
A=7.3
Variables names can contain letters,
numbers but no spaces, only
underscores.
[] square brackets create a matrix.
A_1=[2.5 3.7 4.8]
A_1=[2.5, 3.7, 4.8]
%
=
.
,
;
==
~=
>=
:
10. MATSAB syntax:
; semicolon adds a raw in a two-
dimensional (rectangular) matrix
A_1=[2.5 3.7 4.8]
A_2=[2 3 4; 3 7 8]
Concatenation is a procedure that allows
to combine two matrixes along one
dimension, given the other dimension is
the same
A_3= [A_1; A_2]
A_3= [A_1, A_2]
A_1
A_2
A_3
Matrix concatenation
1X3
2X3
3X3
11. Arithmetic operations
A_3 =
2.5000 3.7000 4.8000
2.0000 3.0000 4.0000
3.0000 7.0000 8.0000
You can add, subtract the same number
form all the elements or to multiply or
divide by the same number.
A4 = A_3*2
A4 =
5.0000 7.4000 9.6000
4.0000 6.0000 8.0000
6.0000 14.0000 16.0000
You can also easily make any of such operations
with two matrices of the same dimensions
element by element
A5 = A_3+A4
A5 =
7.5000 11.1000 14.4000
6.0000 9.0000 12.0000
9.0000 21.0000 24.0000
For devision or maltiplication matlab uses
operators ./ and .*
A6 = A_3 .* A4
A6 =
12.5000 27.3800 46.0800
8.0000 18.0000 32.0000
18.0000 98.0000 128.0000
13. Indexing
A_3 =
2.5000 3.7000 4.8000
2.0000 3.0000 4.0000
3.0000 7.0000 8.0000
A9 = A_3(1)
A9=A_3(3) % select third element of matrix
A9=A_3( [1 2 3 4 5 6] ) % select from first to sixth element of the matrix
A9 =
2.5000 2.0000 3.0000 3.7000 3.0000 7.0000
A9=A_3(3, 2) % select third raw and secodn column
a mnemonic for this might be "Mr Nice guy" - M rows x N columns
A9 =
7
Operators
%
=
.
,
;
==
~=
>=
:
17. [] matrix
Data classes
'' string
{} cell
a.b structure
logical
A13='Kyiv' % city name
A14='Dnipro'
B13=3.4 % population
B14=1.5
Who to combine/concatenate all this information?
Cells or structures!
Cell:
C1={A13, A14; B13, B14}
C1 =
'Kyiv' 'Dnipro'
[3.4000] [1.5000]
Structure must have fields:
S1(1).name=A13
S1(2).name=A14
S1(1).pop=B13
S1(2).pop=B14
S1 =
1x2 struct array with fields:
name
pop
19. The most useful function…
– help (!!)
whos
disp
mean
min
max
length
size
sum
diff
sort
zeros
ones
clear
close
Some
functions:
20. help whos
WHOS List current variables, long form.
WHOS is a long form of WHO. It lists all the variables in the current
workspace, together with information about their size, bytes, class,
etc.
whos
Name Size Bytes Class Attributes
A10 1x5 5 logical
A11 1x3 24 double
A13 1x4 8 char
A14 1x6 12 char
A3 3x3 72 double
A4 3x3 72 double
A5 3x3 72 double
A6 3x3 72 double
A7 3x3 9 logical
A8 3x3 9 logical
A9 1x5 40 double
A_1 1x3 24 double
A_2 2x3 48 double
B13 1x1 8 double
B14 1x1 8 double
C1 2x2 484 cell
S1 1x2 612 struct
a 1x6 48 double
a1 1x6 48 double
21. help disp
DISP Display array.
DISP(X) displays the array, without printing the array name.
In
all other ways it's the same as leaving the semicolon off an
expression except that empty arrays don't display.
If X is a string, the text is displayed.
See also int2str, num2str, sprintf, rats, format.
HELP function helps to use
other functions
disp(C1{1})
Kyiv
22. Typically a function has a name, an input and an output.
output = functionname ( input )
A9 =
1 3 5 7 9
M=mean(A9) % averaging
M =
5
D=diff(A9) % consecutive difference
D =
2 2 2 2
23. Z=zeros(1, 3) % generates [0 0 0]
O=ones(1, 3) % generates [1 1 1]
ZO=[Z,O] % concatenation, gives [0 0 0 1 1 1]
R=repmat(ZO, 1, 2) % replication horizontally two times
Ra=rand(1,12) % random values 0 to 1, 12 elements
P=randperm(12) % values form 1 to 12 in random order
Functions that generate data
26. figure(2)
for i=1:length(RRa)
if RRa(i)>1
plot(i, RRa(i), 'b.')
else
plot(i, RRa(i), 'g.')
end
drawnow
pause(0.5)
if RRa(i)>1.6
break
end;
end
This program draws blue dots at the plot values
if the value is above 1 and green dots at plot
values if the value is below 1.
Loop breaks if the value is above 1.6
27. Saving, clearing and loading data
filename='data' % the prefix of the data file
clk=clock % an array of current time [y m d h m s]
for nf = 1:5;
filename = [filename, '_', sprintf('%02.0f', clk(nf))];
% sprintf prints into a string the date parameters
end
save(filename, 'RRa') % save a variable into an *.m file of a given
name
clear RRa % clear the variable RRa
load(filename) % load the file of the given name
filename =
data_2017_10_09_03
28. Thank you for your interest!
Hopefully soon there will be two more parts:
2. Installation of Psychtoolbox, some demos.
3. Usage of basic Psychtoolbox function for timing,
screen control and keyboard acquisition