# Presenting oval for random duration in Psychtoolbox

**URL:** <https://psychtoolbox.discourse.group/t/presenting-oval-for-random-duration-in-psychtoolbox/4207>\
**Category:** Programming Help\
**Created:** [January 27, 2022, 10:03am UTC](https://psychtoolbox.discourse.group/t/presenting-oval-for-random-duration-in-psychtoolbox/4207 "2022-01-27T10:03:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit27](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Ankit27](https://psychtoolbox.discourse.group/u/Ankit27)\
**Post date:** [January 27, 2022, 10:03am UTC](https://psychtoolbox.discourse.group/t/presenting-oval-for-random-duration-in-psychtoolbox/4207/1 "2022-01-27T10:03:33Z")

</div>

# How to Draw ‘oval’ for 50 ms for 5 times and 350 ms for 5 times “randomly”'. The total number of trials will be 10

Following code I have written in MATLAB, but i am not getting where I am making the mistake. Please do help me immediately regarding this. I would be grateful to you all. Thank You.

CODE WRITTEN IN MATLAB

```matlab
% Clear the workspace and the screen
Screen('Preference', 'SkipSyncTests', 1);
sca;
close all;
clear;
% Delay_time is a variable that will take up 10 random values among 0.05s and 0.35s
Delay_time = zeros(1,10);
Delay_time (1:5) = 0.05;
Delay_time (6:10) = 0.35;
Delay_time = Delay_time (randperm(10));

% Here we call some default settings for setting up Psychtoolbox
PsychDefaultSetup(2);
% Get the screen numbers
screens = Screen('Screens');
% Draw to the external screen if avaliable
screenNumber = max(screens);
% Define black and white
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
% Open an on screen window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
% Make a base Rect of 200 by 250 pixels
baseRect = [0 0 200 250];
% For Ovals we set a miximum diameter up to which it is perfect for
maxDiameter = max(baseRect) * 1.01;
% Center the rectangle on the centre of the screen
centeredRect = CenterRectOnPointd(baseRect, xCenter, yCenter);
% Set the color of the rect to red
rectColor = [1 0 0];

for i_trial = 1:10  
    % Draw the rect to the screen
    Screen('FillOval', window, rectColor, centeredRect, maxDiameter);
    % Flip to the screen
    Screen('Flip', window);
    WaitSecs (Delay_time (i_trial));
end

% Clear the screen
sca;

```

On the output screen, i see only one time oval is displayed. The loop is not working

---

<div class="post-metadata">

**Author:** ![benoitberanger](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@benoitberanger](https://psychtoolbox.discourse.group/u/benoitberanger)\
**Post date:** [February 2, 2022, 11:44am UTC](https://psychtoolbox.discourse.group/t/presenting-oval-for-random-duration-in-psychtoolbox/4207/2 "2022-02-02T11:44:57Z")

</div>

You forgot to show an “empty screen” between ovals :

```auto
for i_trial = 1:10
% Draw the rect to the screen
Screen(‘FillOval’, window, rectColor, centeredRect, maxDiameter);
% Flip to the screen
Screen(‘Flip’, window);
WaitSecs (Delay_time (i_trial));
Screen(‘Flip’, window); % empty screen (nothing drawn in the backbuffer)
WaitSecs (1.0); % wait a bit
end

```

---

<div class="post-metadata">

**Author:** ![Ankit27](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Ankit27](https://psychtoolbox.discourse.group/u/Ankit27)\
**Post date:** [February 3, 2022, 6:45am UTC](https://psychtoolbox.discourse.group/t/presenting-oval-for-random-duration-in-psychtoolbox/4207/3 "2022-02-03T06:45:27Z")

</div>

Thanks a lot benoitberanger !! It’s working now

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [February 3, 2022, 4:43pm UTC](https://psychtoolbox.discourse.group/t/presenting-oval-for-random-duration-in-psychtoolbox/4207/4 "2022-02-03T16:43:02Z")

</div>

Also, using WaitSecs fot visual presentation timing is an invitation to poor timing. See our intro tutorials and demos on how to do proper visual timing with `Screen('Flip')` parameters.  
-mario
