HOW TO USE FOR LOOP FOR MY PROGRAM
7 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
Voss
2021-12-20
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 个评论
Voss
2022-1-27
@ANKIT MAURYA: What is the behavior you observe when you run this code, and how is that behavior different from what you expect?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image display and manipulation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!