Alphabet T and L are overlapping in some of the trials in this code for Psychtoolbox. How to resolve this issue ?
4 views (last 30 days)
Show older comments
I am trying to design an experiment in a Psychtoolbox. I want to display 12 alphabtes on the screen in one trial of the experiment, which includes 11 L alphabets and one T alphabet to appear on the screen. I have specified 40 individual coordinates (x and y values) where the alphabets can appear randomly on the screen.
One of the conditions of the experiment is that the T alphabet cannot appear on the first 8 coordinates in the array. That is, alphabet T cannot appear on the following set of coordinates : xCoordinates = [1056, 1027.9, 960, 892.1, 864, 892.1, 960, 1027.9] and yCoordinates = [540, 472.1, 444, 472.1, 540, 607.9, 636, 607.9]. Apart from these 8 coordinates, T can appear anywhere in the remaining 32 set of coordinates in a randomized order in each trail.
As for the 11 L alphabtes, they can choose to appear at any of the given 40 coordinates randomly.
The problem with my code is that in some of the trials out of total number of trials, alphabet T and one of the Ls appear on the same coordinate. That is, they end up sharing a set of coordinate and appear together. It is required that all the 12 alphabtes (1 T and 11 Ls) appear at 12 separate coordinates, where Ls can appear at any of the 40 coordinates and T can appear at any of the 32 coordinates excluding the first 8 sets of coordinates in the array.
Question:
How can i tranform this code so that: Alphabet T and L do not share a set of coordinate in any of the trials and that each alphabet appears at a specific coordinate without any overlapping? (at the same time making sure that T does not appear at the locatons specified by the first 8 sets of coordinates)
Code for reference:
% Initialize Psychtoolbox
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 1);
screenNumber = max(Screen('Screens'));
black = BlackIndex(screenNumber);
white = WhiteIndex(screenNumber);
grey = white / 2;
% Open a black window
windowColor = black; % Set the background color to black
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, windowColor);
textSize = 36;
textColor = [255, 255, 255]; % Set the text color to white
alphabets = {'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'T', 'L', 'L', 'L'};
% Define x and y coordinates for the letters
xCoordinates = [1056, 1027.9, 960, 892.1, 864, 892.1, 960, 1027.9, 1152, 1095.8, 960, 824.2, 768, 824.2, 960, 1095.8, 1248, 1163.6, 960, 756.4, 672, 756.4, 960, 1163.6, 1344, 1231.5, 960, 688.5, 576, 688.5, 960, 1231.5, 1440, 1299.4, 960, 620.6, 480, 620.6, 960, 1299.4];
yCoordinates = [540, 472.1, 444, 472.1, 540, 607.9, 636, 607.9, 540, 404.2, 348, 404.2, 540, 675.8, 732, 675.8, 540, 336.4, 252, 336.4, 540, 743.6, 828, 743.6, 540, 268.5, 156, 268.5, 540, 811.5, 924, 811.5, 540, 200.6, 60, 200.6, 540, 879.4, 1020, 879.4];
% Number of trials
numTrials = 15;
for trial = 1:numTrials
% Randomly shuffle the order of alphabets and coordinates for each trial
randomizedOrder = randperm(length(alphabets));
shuffledAlphabets = alphabets(randomizedOrder);
shuffledCoordinates = [xCoordinates; yCoordinates];
shuffledCoordinates = shuffledCoordinates(:, randperm(size(shuffledCoordinates, 2)));
% Wait for a keypress to display all letters at once
KbStrokeWait;
for i = 1:length(alphabets)
currentAlphabet = shuffledAlphabets{i};
% Get the x and y coordinates for the current alphabet
if strcmp(currentAlphabet, 'T')
% Exclude the specified coordinates for 'T'
availableCoordinatesT = find(~ismember(xCoordinates, excludeXCoordinates) & ~ismember(yCoordinates, excludeYCoordinates));
randomIndexT = availableCoordinatesT(randi(length(availableCoordinatesT)));
x = xCoordinates(randomIndexT);
y = yCoordinates(randomIndexT);
else
% Exclude the coordinates where 'T' will appear
excludeXCoordinatesL = xCoordinates(strcmp(alphabets, 'T'));
excludeYCoordinatesL = yCoordinates(strcmp(alphabets, 'T'));
availableCoordinatesL = find(~ismember(xCoordinates, excludeXCoordinatesL) & ~ismember(yCoordinates, excludeYCoordinatesL));
randomIndexL = availableCoordinatesL(randi(length(availableCoordinatesL)));
x = shuffledCoordinates(1, i);
y = shuffledCoordinates(2, i);
end
% Draw the text
DrawFormattedText(window, currentAlphabet, x, y, white);
end
% Flip the screen to display all letters at once
Screen('Flip', window);
% Wait for a keypress to proceed
KbWait;
% Clear the screen for the next trial
Screen('FillRect', window, windowColor); % Clear the screen to black
Screen('Flip', window);
% Add a brief pause between trials (you can adjust the duration)
WaitSecs(0);
end
sca;:
0 Comments
Accepted Answer
Jeff Miller
on 13 Feb 2024
I think you have a lot more randomization and complexity than you need. For example, it doesn't matter what order you draw the letters in since they are all displayed at the same time. How about just something like this:
nLs = 11;
for trial = 1:numTrials
Tpos = randi(32); % Pick a random position for the T
Lpos = randperm(40,nLs+1); % Pick random positions for the L's; pick an extra one in case one matches Tpos
Lpos = Lpos(Lpos ~= Tpos); % Throw out the Lpos that matches Tpos, if any
x = xCoordinates(Tpos);
y = yCoordinates(Tpos);
DrawFormattedText(window, 'T', x, y, white);
for iAlphabet=1:nLs
x = xCoordinates(Lpos(iAlphabet));
y = yCoordinates(Lpos(iAlphabet));
DrawFormattedText(window, 'L', x, y, white);
end
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!