Okay, no we are going to learn to display a simple stimulus, a circle.
Open a new script. We will start by using our standard start of script commands:
clc;
clear all;
close all;
Next, we will add a new command:
rng('shuffle');
This command seeds the random number generator so any generated numbers are truly "random". More on this HERE.
From this tutorial onwards, I will simply assume that you have placed these lines at the start of your script. In my lab we do this for every script we run as it is a good habit.
To draw things with the PSYCHTOOLBOX, you first need to open a drawing window. The commands to do this are:
Screen('Preference', 'SkipSyncTests', 1);
[window, window_size] = Screen('OpenWindow', 0, [0 0 0], [],32,2);
Let's pull this apart.
The Screen command is at the heart of all drawing with PSYCHTOOLBOX. You will see a lot of it.
The first command tells Screen to skip any synchronization tests. Ideally, you actually want to leave this line out. However, it will most likely take you a lot of hardware tweaking to get your system to run without this command.
The next command is telling the Screen function to open a new drawing window that will be referred to as window 0. Next, we are setting the background colour to [0 0 0] or black. The next set of brackets [ ] with nothing in them specify the size of the drawing window. By leaving them empty, you are telling Screen to use the full display. the 32 and 2 are default values we always use. This function also returns to values, window which is the window number that refers to this specific drawing window (a pointer if you will as you can have more than one) and window_size which is the screen size in pixels. It actually returns four digits, the first two are the x and y coordinates of the top left corner, the second two are the x and y of the bottom right corner. The first two are always 0,0 unless you specify a different drawing window location.
You can learn more about them by using the following command at the Command Prompt:
Screen('OpenWindow?')
Try that now. This is how you get specific help for PSYCHTOOLBOX commands.
Now, do not open a drawing window from the command line!!! Psychtoolbox will open the window but you will lose your ability to enter commands and you will have to crash out of MATLAB.
Drawing the actual circle is easy, use the following command:
Screen('FillOval', window , [255 0 0], [100 100 200 200]);
This command draws an Oval shape in the drawing window. It gives it a colour of [255 0 0]. Go HERE to learn more about the RGB colour index system. The oval size is determined by [100 100 200 200]. This is a square with a top left location of 100,100 and a bottom right location of 200,200. The oval is drawn to fit within that square.
Now, we need three more commands. The first is:
Screen('Flip',window);
The flip command takes everything that has been drawn and actually pushes it to the display. Without the flip command the images you draw will remain offscreen!
The next is:
WaitSecs(3);
This is important. You have to tell MATLAB how long you want the image on the screen.
Last, you we need to close the drawing window to regain control of MATLAB.
Screen('CloseAll');
It is very important to always end a script with this command if drawing is involved to send control of the screen back to MATLAB.
Try running your code now. If you want to see my version, here is it my_experiment.m
Now, lets try a few activities so you really understand this code.
1. Set the size of the Screen window to a smaller size and run your code.
[window, window_size] = Screen('OpenWindow', 0, [0 0 0], [0 0 400 400],32,2);
This can be really handy for debugging new scripts. Try a few values other than [0 0 400 400].
2. Change the size and colour of the circle being drawn. For example, try:
Screen('FillOval', window , [0 255 0], [200 200 250 250]);
Try some more values on your own.
3. Try drawing more than one circle at a time:
Screen('FillOval', window , [0 255 0], [200 200 250 250]);
Screen('FillOval', window , [0 0 255], [400 200 450 250]);
Screen('Flip',window);
Again, try some more on your own.
4. Change the value in WaitSecs to a different amount. I will leave this one up to you!
5. Try drawing two circles in a row:
Screen('FillOval', window , [0 255 0], [200 200 250 250]);
Screen('Flip',window);
WaitSecs(3);
Screen('FillOval', window , [0 0 255], [400 200 450 250]);
Screen('Flip',window);
WaitSecs(3);
Try a few more sequences now.
The last thing we will tackle here is using variables to handle all drawing sizes.
For example, we can compute the middle of the screen quite easily by using the variable window_size. The x coordinate of the middle of the screen is:
x_middle = window_size(3) / 2;
y_middle = window_size(4) / 2;
Recall that window_size has four values in it. The first two are the x and y values of the top left corner. The 3rd and 4th values are the x and y coordinates of the bottom right corner. So, window_size(3) is the x value and window_size(4) is the y value of the bottom right corner. Thus window_size(3)/2 and window_size(4)/2 refer to the middle of the screen.
Screen('FillOval', window , [255 0 0], [x_middle-20 y_middle-20 x_middle+20 y_middle+20]);
So, this command would draw a circle in the middle of the screen with a radius of 20 pixels. To be even more effective we would make 20 a variable we can set. So:
stim_size = 20;
Screen('FillOval', window , [255 0 0], [x_middle-stim_size y_middle-stim_size x_middle+stim_size y_middle+stim_size]);
Try this now. If you are having trouble, here is some sample code my_experiment2.m
Try playing around with this code to change the drawing size and location using variables as opposed to numbers.
Finally, one last thing. What if you want to draw a square? Simple:
Screen('FillRect', window , [255 0 0], [x_middle-stim_size y_middle-stim_size x_middle+stim_size y_middle+stim_size]);
You can move onto the next tutorial now.
Open a new script. We will start by using our standard start of script commands:
clc;
clear all;
close all;
Next, we will add a new command:
rng('shuffle');
This command seeds the random number generator so any generated numbers are truly "random". More on this HERE.
From this tutorial onwards, I will simply assume that you have placed these lines at the start of your script. In my lab we do this for every script we run as it is a good habit.
To draw things with the PSYCHTOOLBOX, you first need to open a drawing window. The commands to do this are:
Screen('Preference', 'SkipSyncTests', 1);
[window, window_size] = Screen('OpenWindow', 0, [0 0 0], [],32,2);
Let's pull this apart.
The Screen command is at the heart of all drawing with PSYCHTOOLBOX. You will see a lot of it.
The first command tells Screen to skip any synchronization tests. Ideally, you actually want to leave this line out. However, it will most likely take you a lot of hardware tweaking to get your system to run without this command.
The next command is telling the Screen function to open a new drawing window that will be referred to as window 0. Next, we are setting the background colour to [0 0 0] or black. The next set of brackets [ ] with nothing in them specify the size of the drawing window. By leaving them empty, you are telling Screen to use the full display. the 32 and 2 are default values we always use. This function also returns to values, window which is the window number that refers to this specific drawing window (a pointer if you will as you can have more than one) and window_size which is the screen size in pixels. It actually returns four digits, the first two are the x and y coordinates of the top left corner, the second two are the x and y of the bottom right corner. The first two are always 0,0 unless you specify a different drawing window location.
You can learn more about them by using the following command at the Command Prompt:
Screen('OpenWindow?')
Try that now. This is how you get specific help for PSYCHTOOLBOX commands.
Now, do not open a drawing window from the command line!!! Psychtoolbox will open the window but you will lose your ability to enter commands and you will have to crash out of MATLAB.
Drawing the actual circle is easy, use the following command:
Screen('FillOval', window , [255 0 0], [100 100 200 200]);
This command draws an Oval shape in the drawing window. It gives it a colour of [255 0 0]. Go HERE to learn more about the RGB colour index system. The oval size is determined by [100 100 200 200]. This is a square with a top left location of 100,100 and a bottom right location of 200,200. The oval is drawn to fit within that square.
Now, we need three more commands. The first is:
Screen('Flip',window);
The flip command takes everything that has been drawn and actually pushes it to the display. Without the flip command the images you draw will remain offscreen!
The next is:
WaitSecs(3);
This is important. You have to tell MATLAB how long you want the image on the screen.
Last, you we need to close the drawing window to regain control of MATLAB.
Screen('CloseAll');
It is very important to always end a script with this command if drawing is involved to send control of the screen back to MATLAB.
Try running your code now. If you want to see my version, here is it my_experiment.m
Now, lets try a few activities so you really understand this code.
1. Set the size of the Screen window to a smaller size and run your code.
[window, window_size] = Screen('OpenWindow', 0, [0 0 0], [0 0 400 400],32,2);
This can be really handy for debugging new scripts. Try a few values other than [0 0 400 400].
2. Change the size and colour of the circle being drawn. For example, try:
Screen('FillOval', window , [0 255 0], [200 200 250 250]);
Try some more values on your own.
3. Try drawing more than one circle at a time:
Screen('FillOval', window , [0 255 0], [200 200 250 250]);
Screen('FillOval', window , [0 0 255], [400 200 450 250]);
Screen('Flip',window);
Again, try some more on your own.
4. Change the value in WaitSecs to a different amount. I will leave this one up to you!
5. Try drawing two circles in a row:
Screen('FillOval', window , [0 255 0], [200 200 250 250]);
Screen('Flip',window);
WaitSecs(3);
Screen('FillOval', window , [0 0 255], [400 200 450 250]);
Screen('Flip',window);
WaitSecs(3);
Try a few more sequences now.
The last thing we will tackle here is using variables to handle all drawing sizes.
For example, we can compute the middle of the screen quite easily by using the variable window_size. The x coordinate of the middle of the screen is:
x_middle = window_size(3) / 2;
y_middle = window_size(4) / 2;
Recall that window_size has four values in it. The first two are the x and y values of the top left corner. The 3rd and 4th values are the x and y coordinates of the bottom right corner. So, window_size(3) is the x value and window_size(4) is the y value of the bottom right corner. Thus window_size(3)/2 and window_size(4)/2 refer to the middle of the screen.
Screen('FillOval', window , [255 0 0], [x_middle-20 y_middle-20 x_middle+20 y_middle+20]);
So, this command would draw a circle in the middle of the screen with a radius of 20 pixels. To be even more effective we would make 20 a variable we can set. So:
stim_size = 20;
Screen('FillOval', window , [255 0 0], [x_middle-stim_size y_middle-stim_size x_middle+stim_size y_middle+stim_size]);
Try this now. If you are having trouble, here is some sample code my_experiment2.m
Try playing around with this code to change the drawing size and location using variables as opposed to numbers.
Finally, one last thing. What if you want to draw a square? Simple:
Screen('FillRect', window , [255 0 0], [x_middle-stim_size y_middle-stim_size x_middle+stim_size y_middle+stim_size]);
You can move onto the next tutorial now.