HOW TO USE FOR LOOP FOR MY PROGRAM

I want to display any image stored in my computer for 500 ms and after that there should be fixation cross for 500 ms on the screen. Till this point i have written the code but now i have to run the same sequence of stimuli for '10 times' using a for loop. Please help me with this. Even after knowing the syntax of for loop, i am unable to do this as i am very new to programming and matlab platform. Please help me precisely and in relation to my code written below. If possible please write the for loop code for me in relation to the code written below. I would be very grateful to you all for your suggestions. Thank You
Code:
% Clear the workspace and the screen
sca;
close all;
clear;
% 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);
% Query the frame duration
ifi = Screen('GetFlipInterval', window);
% Set up alpha-blending for smooth (anti-aliased) lines
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Setup the text type for the window
Screen('TextFont', window, 'Ariel');
Screen('TextSize', window, 36);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
Here we load in an image from file. This one is a image of rabbits that
% is included with PTB
theImageLocation = [PsychtoolboxRoot 'PsychDemos' filesep...
'AlphaImageDemo' filesep 'IIT.jpg'];
theImage = imread(theImageLocation);
% Get the size of the image
ankit = size(theImage);
% Make the image into a texture
imageTexture = Screen('MakeTexture', window, theImage);
% Draw the image to the screen, unless otherwise specified PTB will draw
% the texture full size in the center of the screen. We first draw the
% image in its correct orientation.
Screen('DrawTexture', window, imageTexture, [], [], 0);
% Flip to the screen
Screen('Flip', window);
% % Wait for two seconds
WaitSecs(1);
% KbStrokeWait;
% Here we set the size of the arms of our fixation cross
fixCrossDimPix = 20;
% Now we set the coordinates (these are all relative to zero we will let
% the drawing routine center the cross in the center of our monitor for us)
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords = [xCoords; yCoords];
% Set the line width for our fixation cross
lineWidthPix = 4;
% Draw the fixation cross in white, set it to the center of our screen and
% set good quality antialiasing
Screen('DrawLines', window, allCoords,...
lineWidthPix, white, [xCenter yCenter], 2);
% Flip to the screen
Screen('Flip', window);
% Wait for a key press
WaitSecs (1);
% KbStrokeWait;
% Clear the screen
sca;

 Accepted Answer

Maybe something like this would work:
% Clear the workspace and the screen
sca;
close all;
clear;
% 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);
% Query the frame duration
ifi = Screen('GetFlipInterval', window);
% Set up alpha-blending for smooth (anti-aliased) lines
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Setup the text type for the window
Screen('TextFont', window, 'Ariel');
Screen('TextSize', window, 36);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
% Here we load in an image from file. This one is a image of rabbits that
% is included with PTB
theImageLocation = [PsychtoolboxRoot 'PsychDemos' filesep...
'AlphaImageDemo' filesep 'IIT.jpg'];
theImage = imread(theImageLocation);
% Get the size of the image
ankit = size(theImage);
% Make the image into a texture
imageTexture = Screen('MakeTexture', window, theImage);
% Here we set the size of the arms of our fixation cross
fixCrossDimPix = 20;
% Now we set the coordinates (these are all relative to zero we will let
% the drawing routine center the cross in the center of our monitor for us)
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords = [xCoords; yCoords];
% Set the line width for our fixation cross
lineWidthPix = 4;
for i = 1:10
% Draw the image to the screen, unless otherwise specified PTB will draw
% the texture full size in the center of the screen. We first draw the
% image in its correct orientation.
Screen('DrawTexture', window, imageTexture, [], [], 0);
% Flip to the screen
Screen('Flip', window);
% % Wait for two seconds
WaitSecs(0.5); % 500 ms, as stated
% KbStrokeWait;
% Draw the fixation cross in white, set it to the center of our screen and
% set good quality antialiasing
Screen('DrawLines', window, allCoords,...
lineWidthPix, white, [xCenter yCenter], 2);
% Flip to the screen
Screen('Flip', window);
% Wait for a key press
WaitSecs (0.5); % 500 ms, as stated
% KbStrokeWait;
% Clear the screen
sca;
end

5 Comments

This worked . Thanks a lot . Only thing that was a minute mistake was that 'end' of for loop should come before 'clear the screen'
Like this :
end
% Clear the screen
sca;
But again, thanks a lot Benjamin
That makes sense, I'm glad you found and fixed that. I don't have PsychToolbox, so I wasn't able to run the code myself.
Can you please help me out with the following problem. I need your help so please help me with this one.
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, 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:
% 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;
@ANKIT MAURYA: What is the behavior you observe when you run this code, and how is that behavior different from what you expect?
Benjamin, i would like to inform you that I am able to solve this query now. Adding a blank screen was missing ater displaying oval.
But many many thanks for your quick reply

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Asked:

on 20 Dec 2021

Commented:

on 3 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!