MATLAB part III (introduction to Psychtoolbox)

RA tutorial week 8, summer 2019

shelby bachman

Overview

week topic
may 24 literature search & reference management
may 31 R part I: syntax & data types
jun 7 R part II: data import & cleaning with dplyr
jun 14 R part III: tidy data & plotting with ggplot2
jun 21 R part IV: review & writing a script
jun 27 MATLAB part I: syntax, data types & operations
jul 5 no tutorial
jul 12 MATLAB part II: control flow, scripts & functions
jul 19 MATLAB part III: intro to Psychtoolbox
jul 26 no tutorial
aug 2 MATLAB part IV: building a basic experiment in Psychtoolbox
aug 9 data lab: collecting some data using matlab & Psychtoolbox
aug 16 data lab: tidying & analyzing our collected data in R

Today

  • Psychtoolbox
    • overview
    • installation
    • key concepts
  • Writing our experiment
    • overview of components
    • setting parameters
    • opening a screen

Psychtoolbox

Psychtoolbox is a software package that adds low-level control of stimulus presentation to interpreted languages such as MATLAB.

Advantages of PTB:

  • Precise and flexible commands for:
    • timing
    • response collection
    • device control
  • Relatively simple to learn if you are comfortable with MATLAB
  • Widely used in psychology & cognitive neuroscience
  • Large online community of users

Psychtoolbox (cont.)

Disadvantages of PTB:

  • Less and less support for Mac OS
  • Complicated to move code from one operating system to another
    • or one computer to another
  • Code-only interface
    • as opposed a builder interface (e.g. E-Prime)

One alternative that I personally like is PsychoPy, which combines both a coder and builder interface and is easy to transfer from one operating system to another.

Alternatively, jspsych is a javascript-based library for running experiments in the browser.

Installing Psychtoolbox

If you are using a Mac:

  • Navigate here
  • Download by clicking Clone or download --> Download ZIP, and unzip the downloaded folder
  • Navigate into the downloaded folder
  • Copy the directory Psychtoolbox into your Applications folder
  • Open MATLAB
  • cd into the Psychtoolbox directory in your Applications folder
  • Enter the following command and press Enter: SetupPsychToolbox

If you are using Windows:

Please follow the instructions at this link.

Adding Psychtoolbox to your path

Once Psychtoolbox is installed, you'll want to add it to your MATLAB path. Adding a file or set of files to your path means that MATLAB will know where to "look" for those files.

To add the Psychtoolbox directory and its subfolders to your MATLAB path:

  • Home Tab --> Set Path --> Add with Subfolders -->
  • Find Psychtoolbox directory and add it
  • Save the result

(If you ran the SetupPsychtoolbox script in the last section, this should already be done. Check that Psychtoolbox is added to your path.)

Psychtoolbox concepts

  • Drawing a screen
  • Collecting and deciphering keypress responses
  • Controlling and measuring timing

Drawing a screen

RGB color codes

Positioning things on the screen


[x1 y1 x2 y2]

Collecting keypress responses

We are often going to be collecting responses in our experiments using the keyboard. When using Psychtoolbox, we will use a system in which each key has a unique code.

For instance, the key j has a code of 13.

We use the KbName() function to retrieve either the code associated with a letter:

KbName('j')

the letter associated with a code:

KbName(13)

By default, different operating systems have different key codes. We will "unify" them across operating systems using the KbUnifyNames when setting the parameters for our experiments.

Collecting keypress responses (cont.)


[keyIsDown, secs, keyCode] = KbCheck;

keyIsDown: was a key pressed? (1=yes, 0=no) secs: timestamp when the key was pressed keyCode: a vector of 1/0s for all keys; the value for the key that was pressed is 1

Let's get started using Psychtoolbox

Download the zip file containing script for this week. Open all the scripts in MATLAB, and let's get started!

Next week

There is no tutorial next week! We will resume on August 2 at 10:30am to start building our stroop task in Psychtoolbox.