![Page 1: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/1.jpg)
Matlab Class 5
Xiaotao Su, Ph.D.Visual Psychophysicist & IT Group Leader
Rutgers Center for Cognitive Science (RuCCS)
![Page 2: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/2.jpg)
RuCCS
Psychtoolbox Basics
• Computers are perfect for creating and displaying visual psychophysics stimuli
• Programs usually written in a low-level language (e.g. C or Pascal) to achieve full control of the hardware for precise stimulus display but are difficult to program
• Interpreted languages like Matlab are abstracted from hardware details and provide friendlier development environments
• The Psychophysics Toolbox is a software package that allows Matlab to fully control the hardware for precise stimulus display while retaining the flexibility and ease of Matlab.
![Page 3: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/3.jpg)
RuCCS
Screen drawing and psychtoolbox
Screen Geometry
X ----------- Positive ->
Y
Positive
Origin
Max X and Y
Coordinates are measured in pixels.
![Page 4: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/4.jpg)
RuCCS
How you typically work with the psychtoolbox
Back Buffer(invisible)
Front Buffer
![Page 5: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/5.jpg)
RuCCS
How you typically work with the psychtoolbox
Front Buffer
Step 1: DrawShape to the back buffer
![Page 6: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/6.jpg)
RuCCS
How you typically work with the psychtoolbox
Step 2:Flip the back bufferto the front buffer
![Page 7: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/7.jpg)
RuCCS
How you typically work with the psychtoolbox
Back Buffer is automatically cleared
![Page 8: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/8.jpg)
RuCCS
How you typically work with the psychtoolbox
Now you can continue with your next frame of animation
![Page 9: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/9.jpg)
RuCCS
How you typically work with the psychtoolbox
Flip the back bufferto the front buffer
![Page 10: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/10.jpg)
RuCCS
How you typically work with the psychtoolbox
Back Buffer is automatically cleared
![Page 11: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/11.jpg)
RuCCS
Basic Display Loop in PsychToolbox (PTB)
% Open Window
% while some condition is true do the following
% draw something
% make some changes
% repeat
% Close screen
![Page 12: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/12.jpg)
RuCCS
Set up program draw_stuff
% draw_stuff.m
%
% Draws stuff on the screen
![Page 13: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/13.jpg)
RuCCS
Skip Sync Tests
• Add to your program before opening the new screen
Screen('Preference','SkipSyncTests', 1);
![Page 14: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/14.jpg)
RuCCS
Initialize the main window
% draw_stuff.m%% Draws stuff on the screenclear all;which_screen=0;
![Page 15: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/15.jpg)
RuCCS
Set up program draw_stuff
% draw_stuff.m%% Draws stuff on the screenclear all;which_screen=0;
[window_ptr, screen_dimensions]=Screen(0,'OpenWindow', [0, 0, 0]);
0 is main screen (allows for multiple monitors)
Command issued to the psychtoolbox Screen function
[red, green, blue]
Vector specifyingScreen color
0
![Page 16: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/16.jpg)
RuCCS
Set up program draw_stuff
% draw_stuff.m%% Draws stuff on the screenclear all;which_screen=0;[window_ptr, screen_dimensions]=Screen(0,'OpenWindow', [0, 0, 0]);
Vector [x1, y1, x2, y2]Which could be:[0, 0, 800, 600]For an 800 X 600 display
x1= 0y1 = 0
x2= 800 y2 = 600
![Page 17: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/17.jpg)
RuCCS
Draw shape in back buffer% draw_stuff.m%% Draws stuff on the screenclear all;which_screen=0;[window_ptr, screen_dimensions]=Screen(0,'OpenWindow', [0, 0, 0]);
shape_dimensions = screen_dimensions/2;Screen(window_ptr, 'FillRect', [0,0,255], shape_dimensions);
X1, Y1
X2, Y2Front buffer
Back buffer
![Page 18: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/18.jpg)
RuCCS
% draw_stuff.m%% Draws stuff on the screenclear all;which_screen=0;[window_ptr, screen_dimensions]=Screen(0,'OpenWindow', [0, 0, 0]);shape_dimensions = screen_dimensions/2;Screen(window_ptr, 'FillRect', [0,0,255], shape_dimensions);%Flip the buffersScreen(window_ptr,'Flip');
Back buffer
Flip the back buffer to the front buffer
Front buffer
![Page 19: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/19.jpg)
RuCCS
Clear screen<<<<snip>>>>clear all;which_screen=0;[window_ptr, screen_dimensions]=Screen(0,'OpenWindow', [0, 0, 0]);
shape_dimensions = screen_dimensions/2;Screen(window_pointer, 'FillRect', [0,0,255], shape_dimensions);%Copy to main windowScreen(window,'Flip');%leave the image up for 5 secondsWaitSecs(5);%Clear screen and return control to matlabclear screen;
![Page 20: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/20.jpg)
RuCCS
Task 1: Making a movie% draw_stuff.m%% Draws stuff on the screenclear all;which_screen=0;[window_ptr, screen_dimensions] …=Screen(0,'OpenWindow', [0, 0, 0]);
shape_dimensions = screen_dimensions/2;Screen(window_pointer, 'FillRect', … [0,0,255], shape_dimensions);
% then loop and increment screen_dimensions at each turn
![Page 21: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/21.jpg)
RuCCS
Task 1: Basic Display Loop in PTB (code)
% draw_stuff.m%% Draws stuff on the screenclear all;try
which_screen=0;bg_color = [0, 0, 0];[window_ptr, screen_dimensions]= …
Screen(which_screen, …
'OpenWindow',bg_color);shape_dimensions = … screen_dimensions/2;tic;
while (toc < 5) % move shape shape_dimensions = …
shape_dimensions + [0,5,0,5];
% draw shape Screen(window_ptr, 'FillRect', … [0,0,255], shape_dimensions);
% flip buffer and repeat Screen(‘Flip’, window_ptr);endclear Screen;
catch
clear Screen;
end
![Page 22: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/22.jpg)
RuCCS
Moving Shapes around the Screen
• Shapes are defined by an array of numbers e.g. sh1 = [10, 15, 40, 45] ; % [x1, y1, x2, y2]
• To move a shape in the x direction by 5 pixels we need to increment both x coordinates by 5e.g. sh1(1) = sh1(1) +5; sh1(3) = sh1(3) +5
or use another array: sh1 = sh1 + [5, 0, 5, 0];
• Same for moving in the y direction
• Increment both if you want to move diagonally
![Page 23: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/23.jpg)
RuCCS
Task 2
Make the rectangle move to the bottom of the screen at 5 pixels per frame, then move right when it hits the bottom.When it hits the right, the program ends
Hint: shape_dimensions(1) is x1, (2) is y1, (3) is x2, 4 is y2
![Page 24: Matlab Class 5 Xiaotao Su, Ph.D. Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)](https://reader034.vdocument.in/reader034/viewer/2022042717/56649c755503460f94929c57/html5/thumbnails/24.jpg)
RuCCS
Task 3
When the rectangle hits the bottom make it change colors to red
Hint: color is a 3 number vector red is [255, 0, 0]