﻿

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.

## Requirements

• A graphic card with two monitor outputs
• A SLM with a DVI cable

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

## 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 a 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:

Screen(window,'Flip');

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);

Screen(window,'Flip');

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);

end

An then send them on the SLM:

for i = 1:30

Screen('DrawTexture', window, texture(i));

Screen(window,'Flip');

end

Finally, close the window using:

Screen('CloseAll');

#11 G. de Montmirail 2019-09-30 12:25
Hi Sébastien,

Thank you for this usefull website.
I have an issue with displaying the phase on the SLM with Matlab. The SLM I am using has a dimension of 1280x1024 pixels. So I calculate the phase as a 1280x1024 matrix and I display it on the SLM as a second screen. The problem is that there is a white border all around the calculated phase (I can see it when I use a second screen of the same size as the SLM). So the displayed phase has a size decreased, around 1000*800 pixels...It means that all the "white" pixels on the borders are not used for diffraction, all set at 255 (2pi).
I followed all the steps you mentionned but I can't fix this..
Thank you

#10 Sébastien Popoff 2018-07-05 08:28
Hi Rubio,

The fact that the SLM is connected via HDMI, VGA or DVI is not relevant.
Psychtoolbox would work with any device recognized as a secondary screen. You have to make sure that your display is set for an extended desktop (settings of your OS). If your displays (your monitor + your SLM) are set as duplicate, Psychtoolbox detects only one display.

If you have further issues, contact the Psychtoolbox team. I am not related to them and I stopped using it for a while as I do not use Matlab anymore.

Best,

Sebastien

Quoting Ruibo Shang:
Hi,
Thanks so much for sharing this. Does it also work for the SLM with HDMI cable? I tried to control SLM with HDMI cable. However, there was an error when I ran the command window = Screen('OpenWindow',2); It said Invalid or missing Screen Number or Window Index. Any reason for this? Thanks.

#9 Ruibo Shang 2018-07-02 19:48
Hi,
Thanks so much for sharing this. Does it also work for the SLM with HDMI cable? I tried to control SLM with HDMI cable. However, there was an error when I ran the command window = Screen('OpenWin dow',2); It said Invalid or missing Screen Number or Window Index. Any reason for this? Thanks.

#8 Daniel 2017-04-19 03:09
Quoting Martin M:
Bonjour,
Merci pour ce tuto, malheureusement il ne marche pas pour moi...
Je viens d'installer "Subversion 1.7.x command-line client" et d'enregistrer "Psychtoolbox installer " dans C:\Drivers\toolbox
Je lance matlab en administrateur, puis ">> cd C:\Drivers\toolbox

et j'obtiens un message d'erreur

"'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes.
'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes."

je suis perdu...
pouvez vous m'aider

bien à vous
Martin

#7 Sébastien Popoff 2016-05-05 11:36
Hi,

I never used Cogent Graphics, I did not know about it to be honest, so I cannot compare.

Best,

Sebastien

#6 Ivan Gusachenko 2016-05-05 09:44
Thank you for this! By any chance, have you tried Cogent Graphics library for Matlab before? I think it's quite a similar thing, but maybe not maintained anymore. I'm using Cogent now, but I suspect it causes some instabilities in Matlab. I wonder if it's worth switching to Psychtoolbox, and if you have experience working with both.
Thank you

#5 Sébastien Popoff 2016-03-30 12:48
Sorry I did not reply in time on your issue about installing Psychtoolbox, but I do not think I would have been of any help.
I switched to Python now and the install procedure has change since last time I used Psychtoolbox.

Best,

#4 martin m 2016-03-29 07:51
tout est bon!!!

#3 Martin M 2016-03-24 17:09
Bonjour,
Merci pour ce tuto, malheureusement il ne marche pas pour moi...
Je viens d'installer "Subversion 1.7.x command-line client" et d'enregistrer "Psychtoolbox installer " dans C:\Drivers\toolbox
Je lance matlab en administrateur, puis ">> cd C:\Drivers\toolbox

et j'obtiens un message d'erreur

"'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes.
'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes."

je suis perdu...
pouvez vous m'aider

bien à vous
Martin

#2 Sébastien Popoff 2015-04-27 12:59
Thanks for the info, the link has been updated.