How to control an SLM with Matlab/Octave using Psychtoolbox

Most spatial light modulators (SLMs) available are controllable like a normal computer monitor and are plugged on a computer with a DVI cable.  Some SLMs are now sold with a dedicated card or can be controlled via USB. If you possess such a device, this tutorial is not for you. The first requirement to control the SLM with a DVI/HDMI cable is to have a graphic card with two monitor outputs, one for your screen, one for your SLM. Once plugged to the computer, the SLM is then handled by the operating system as a secondary monitor. No software is required to display an image on the SLM. For that reason, the constructor does not provide any code to use the SLM with Matlab/Octave or other software. One solution to send images with Matlab is to display an array in a figure that fits the size of the secondary monitor. Nevertheless, this technique presents some drawbacks due to the fact that you do not control directly the pixels of the SLM. For instance, the border of the figure, which may be different depending on the operating system, has to be taken into account. More importantly, the scaling of the figure does not guarantee that one pixel of the image displayed corresponds to one pixel of the SLM. For application where a very good resolution is needed, a blurred image on the SLM can be detrimental.

I present how to control directly the pixels of the SLM using Psychtoolbox, a free toolbox for Matlab and Octave that uses GPU acceleration. I show here a tutorial for Matlab, but the toolbox also exists for Octave and seems to work a similar way.


  • A graphic card with two monitor outputs
  • Matlab 7.x or newer
  • An SLM with a DVI cable

Download and install Psychtoolbox

The first step is to download and install the toolbox. Full instructions are to be found on the official website of Psychtoolbox:

PsychToolBox download and installation

Control the SLM

We first need to initialize the toolbox.

window = Screen('OpenWindow',2);

This will create a full-screen window on monitor '2' (the SLM).

You can now retrieve the minimum and maximum pixel values - corresponding to black and white on a normal display.

white = WhiteIndex(window);
black = BlackIndex(window); 

These values should be 0 and 255 for an 8 bits display.

There are different ways to display an image on the SLM. To draw a rectangle of a given value, one can use the function :

Screen(window, 'FillRect',value,[x1,x2,y1,y2])

[x1,x2,y1,y2] being the coordinates of the rectangle. The image is drawn off-screen so far, so nothing is displayed and it can me modify again before being sent on the screen. When finished, display the image on the SLM using the function:


The following code shows how to display an image on the entire SLM of resolution resX by resY.

[x,y] = meshgrid(0:resX-1, 0:resY-1);
image =  sin(0.01*2*pi*x);
Screen(window, 'PutImage',image);

This technique can be slow and decrease the frame rate at which images are displayed. If it is possible to generate the images before sending them on the SLM, it is preferable to load them before in textures.

for i = 1:30
    image =  sin(0.01*2*pi*(x+i/30*100));
    texture(i) = Screen(window, 'MakeTexture', image);

An then send them on the SLM:

for i = 1:30
    Screen('DrawTexture', window, texture(i));

 Finally, close the window using:



Created by sebastien.popoff on 18/04/2013