Control from MATLAB

Contents:

Installation

The standard distribution of mPsy contains a sample experiment in which MATLAB is used to define the timeline of the experiment and its parameters.

Presentation of stimuli requires a working installation of Python as described in Tutorials. Once Python is installed, extract the archived mPsy package into an empty directory.

Sample experiment

Here stimulus presentation is carried by Python. MATLAB is used to control the experiment remotely. Python listens to MATLAB commands and executes them.

The following is a “blank” experiment that presents no stimuli.

from stimuli import *

def main():
    fullscreen = False
    kwargs = {}
    if not fullscreen:
        kwargs = dict(width=1024,height=768)
    win = ExpWindow(fullscreen,**kwargs)

    w, h = win.width, win.height
    cx, cy = win.width //2, win.height //2

    # tell mPsy about remote control
    import web
    remote = web.Remote(5000)
    win.set_remote(remote)
    # capture event by the remote control
    win.set_logger(remote.event)
    
    # run experiment
    # mPsy takes Trials from the begining of the trials list
    # if the trials list is empty mPsy waits
    # ESCAPE stops the experiment at any time
    run()

if __name__ == "__main__":
    main()

Remote control is enabled by the following code.

    # tell mPsy about remote control
    import web
    remote = web.Remote(5000)
    win.set_remote(remote)
    # capture event by the remote control
    win.set_logger(remote.event)

In a complete experiment, the timing and identity of stimuli is controlled from MATLAB. Python presents stimuli, collect subject’s responses, and makes the collected information available for MATLAB to pick up.

The MATLAB side of this experiment looks as follows.

mimport('matlab');

if ~mpsy_alive()
    mpsy_start_safe();
end

win = mpsy_info();
cx = round(win.width/2);
cy = round(win.height/2);

set_background([0.5,0.5,0.5]);

% prepare grating configuration
pleft = Params('width',400.0,'speed',20.0,'fs',40.0);
% p4 will be the same as p3, with speed 40.0
pright = copy_params(pleft,'speed',40.0);

% setup trials
trials = {};

speeds = 10 + 10*rand(6,2);

% setup presentation screens
stimuli_gratings = { Grating([cx-350,cy],pleft), ...
                     Grating([cx+350,cy],pright) };

welcome_line  = { Text([cx,cy-50],Params('msg','Press SPACE when ready.')) };
farewell_line = { Text([cx,cy-50],Params('msg','Game over!')) };
response_line = { Text([cx,cy],Params('msg','Which was faster\nLeft or Right')) };
dot = { Dot([cx,cy],Params('c',0.5)) };

% setup trials
trials = {};  

keys_none    = [];
keys_default = [key.SPACE,key.C,key.N,key.LEFT,key.RIGHT];

% add a message screen, wait until button press (duration=0 means wait)
trials(end+1) = { Trial('Start',welcome_line,0,keys_default) };

% add three Trials each consisting of a Fixation, 
% Stimulus and a Response screemn
for i = 1:3
    trials = [ trials, ...
               Trial('Fixation',             dot, 1000, keys_none   ), ...
               Trial('Gratings',stimuli_gratings, 1500, keys_none   ), ...
               Trial('Response',   response_line,    0, keys_default) ];
end

trials{end+1} = Trial('End', farewell_line, 0, keys_none);

% tell mPsy about trials
set_trials(trials);

% watch the events for 10 seconds
for i = 1:10,
    pause(1.0);
    events = mpsy_events;
    for j = 1:length(events),
        disp(events{j});
    end
end

To run this demonstration:

  • Copy the above snippet into MATLAB (starting with mimport).
  • Run the exp_tut_webremote.py
    • To accomplish this, double-click the file name
    • or execute it by typing python exp_tut_webremote.py in the Terminal.
  • A successfully executed Python program creates an empty black screen
    • or a black window when fullscreen option is off.
  • At this point you can go to MATLAB, change you directory to that containing
    • file exp_tut4.m
    • file mimport.m
    • subdirectory matlab

and run exp_tut4.

The syntax of the MATLAB experiment is modelled after the Python examples. The main tutorial in Tutorials explains how to define parameters of the stimuli and the timeline.

Recording responses

On every event triggered by mPsy (or by subject’s response) information is added to mPsy event log as described in tutorial Control from Python. To catch events in MATLAB we regularly check for the messages accumulating on the Python side:

% watch the events for 10 seconds
for i = 1:10,
    pause(1.0);
    events = mpsy_events;
    for j = 1:length(events),
        disp(events{j});
    end
end