How to randomise trials without following in successive ones

4 views (last 30 days)
Participants are presented with color words written in different ink color. The
participants are required to name the ink color of the stimuli.
Each respond of the participant is recorded as a WAV file in each trial and also the reaction time is recorded.
Every trial includes: a fixation cross presented for 0.5 ms, then the stimulus (color word)presented for 2 seconds and a blank screen presented 0.5 ms. The voice recording and reactiontime calculation occurs during color word presentation.
The experiment includes several categories of color words presented as jpg image files which are placed in different folders.
The code chooses a random image from a folder in each trial. However, there is a
possibility that some stimuli with similar features, or the same exact stimulus could be presentedin successive trials. Thus, there is a need of restricting this possibility.
This could be done by defining some stimulus clusters and writing “if..else” codes
restricting choosing stimuli from the same clusters in successive trials.
The clusters are the following:
BlueInkColor: MSS.JPG; MCS.JPG; MJS.JPG; MZS.JPG; TMM.JPG; TKM.JPG; TSM.JPG; TYM.JPG
GreenInkColor: MZZ.JPG; MCZ.JPG; MJZ.JPG; MSZ.JPG; TYY.JPG; TKY.JPG; TMY.JPG; TSY.JPG
RedInkColor: MCC.JPG; MJC.JPG; MSC.JPG; MZC.JPG; TKK.JPG; TMK.JPG; TSK.JPG; TYK.JPG
YellowInkColor: MJJ.JPG; MCJ.JPG; MSJ.JPG; MZJ.JPG; TSS.JPG; TKS.JPG; TMS.JPG; TYS.JPG;
BlueColorWord: MSS.JPG; MSC.JPG; MSJ.JPG; MSZ.JPG; TMM.JPG; TMK.JPG; TMS.JPG; TMY.JPG
GreenColorWord: MZZ.JPG; MZC.JPG; MZJ.JPG; MZS.JPG; TYY.JPG; TYK.JPG; TYM.JPG; TYS.JPG
RedColorWord: MCC.JPG; MCJ.JPG; MCS.JPG; MCZ.JPG; TKK.JPG; TKM.JPG; TKS.JPG; TKY.JPG
YellowColorWord: MJJ.JPG; MJC.JPG; MJS.JPG; MJZ.JPG; TSS.JPG; TSK.JPG; TSM.JPG; TSY.JPG
Finally, those images who have the exact same name (e.g., MSS.JPG) should not follow in
successive trials.
Here is the part of the code where I need to add the new code to not repeat the trials.The data set contains 64 trials all together.
% Run experimental trials
for exp_i = 1 : length(experiment_order)
% Calculate image position (center of the screen)
fname = [];
if experiment_order{exp_i}{1} == 0
%tr uyumlu
fname= ['TurkceUyumlu/' tr_uyumlu_dosyalar(2+experiment_order{exp_i}{2}).name];
elseif experiment_order{exp_i}{1} == 1
%tr uyumsuz
fname= ['TurkceUyumsuz/' tr_uyumsuz_dosyalar(2+experiment_order{exp_i}{2}).name];
elseif experiment_order{exp_i}{1} == 2
%ing uyumlu
fname= ['ingilizceUyumlu/' ing_uyumlu_dosyalar(2+experiment_order{exp_i}{2}).name];
elseif experiment_order{exp_i}{1}== 3
%ing uyumsuz
fname= ['ingilizceUyumsuz/' ing_uyumsuz_dosyalar(2+experiment_order{exp_i}{2}).name];
end
img = imread(fname);
img_texture = Screen('MakeTexture', myWindow, img);
imageSize = size(img);
pos = [(W-imageSize(2))/2 (H-imageSize(1))/2 (W+imageSize(2))/2 (H+imageSize(1))/2];
% Show fixation cross
drawCross(myWindow,W,H);
tFixation = Screen('Flip', myWindow);
while(GetSecs - tFixation) < fixationDuration
WaitSecs(0.05);
end
  9 Comments
Jon
Jon on 13 Dec 2021
Edited: Jon on 13 Dec 2021
OK, I think I finally got it. Thanks for your patience.
If I am understanding correctly I think this code should do what you are asking, or will come close and you can adapt.
Please let me know if you have any questions on how it works or if I missed something.
% define parameters
numPresentations = 64; % number of presentations
numColorWords = 4; % number of possible word names, to be displayed
numColorInks = 4; % number of possible ink colors to be used
% define folder names, English, Turkish
folderNames = {'ingilizceUyumlu', 'TurkceUyumsuz'}
% define color codes red,green,blue, yelow,
% first row for English, second row for Turkish
% columns are colors
colorCode = {'C','Z','S','J';
'K','Y','M','S'};
% define language code
languageCode = {'M','T'}; % prefix English, Turkish
% generate random seqences of non-repeating color words, and ink colors
% integer values correspond to indices in the color code array
iColorWord = norepeat(numColorWords,numPresentations); % nonrepeating sequence of values between 1 and 4
jColorInk = norepeat(numColorInks,numPresentations); % nonrepeating sequence of values between 1 and 4
% generate random sequence of language, we don't care if this repeats
kLanguage = randi(2,numPresentations); % sequence of 1,2
% generate file paths using folder and color code tables
fileNames = cell(numPresentations,1); % preallocate cell array to hold names
for k = 1:numPresentations
kL = kLanguage(k); % local name to make more readable 1 or 2 for English or Turkish
folder = folderNames{kL};
language = languageCode{kL};
word = colorCode{kL,iColorWord(k)};
ink = colorCode{kL,jColorInk(k)};
file = [language, word, ink,'.JPG']; % for example MSZ.JPG
fileNames{k} = fullfile(folder,file);
end
function r = norepeat(k,n)
% generate n random integers between 1 and k without repetition
% from John D'Errico (Much Thanks!)
% https://www.mathworks.com/matlabcentral/answers/169274-how-can-i-have-random-numbers-with-criteria#answer_164343
r = 1 + mod(cumsum([randi(k),randi(k-1,[1,n-1])]),k);
end
Here is a partial listing of the file names that are generated by a run of the code
'TurkceUyumsuz\TSM.JPG'
'ingilizceUyumlu\MSJ.JPG'
'TurkceUyumsuz\TYM.JPG'
'TurkceUyumsuz\TKY.JPG'
'ingilizceUyumlu\MSC.JPG'
'TurkceUyumsuz\TYY.JPG'
'ingilizceUyumlu\MJC.JPG'
'ingilizceUyumlu\MSS.JPG'
'ingilizceUyumlu\MZC.JPG'
'ingilizceUyumlu\MCZ.JPG'
'ingilizceUyumlu\MSC.JPG'
'TurkceUyumsuz\TKY.JPG'
'TurkceUyumsuz\TMK.JPG'
'TurkceUyumsuz\TKS.JPG'
'TurkceUyumsuz\TYK.JPG'
'ingilizceUyumlu\MJJ.JPG'
'TurkceUyumsuz\TYK.JPG'
'TurkceUyumsuz\TKY.JPG'
'ingilizceUyumlu\MSJ.JPG'
'ingilizceUyumlu\MJC.JPG'
'TurkceUyumsuz\TKS.JPG'
'ingilizceUyumlu\MSZ.JPG'
'TurkceUyumsuz\TSK.JPG'
Amra Feta
Amra Feta on 14 Dec 2021
Omg thank you so much @Jon you saved me really !!! Also a big thanks from my friend. The only thing left is where should I put this in her code like you saw the code because we tried it with her it is still not working for us because we are nott putting it in the right place I guess.

Sign in to comment.

Accepted Answer

Jon
Jon on 14 Dec 2021
OK, I think I finally got it. Thanks for your patience.
If I am understanding correctly I think this code should do what you are asking, or will come close and you can adapt.
Please let me know if you have any questions on how it works or if I missed something.
% define parameters
numPresentations = 64; % number of presentations
numColorWords = 4; % number of possible word names, to be displayed
numColorInks = 4; % number of possible ink colors to be used
% define folder names, English, Turkish
folderNames = {'ingilizceUyumlu', 'TurkceUyumsuz'}
% define color codes red,green,blue, yelow,
% first row for English, second row for Turkish
% columns are colors
colorCode = {'C','Z','S','J';
'K','Y','M','S'};
% define language code
languageCode = {'M','T'}; % prefix English, Turkish
% generate random seqences of non-repeating color words, and ink colors
% integer values correspond to indices in the color code array
iColorWord = norepeat(numColorWords,numPresentations); % nonrepeating sequence of values between 1 and 4
jColorInk = norepeat(numColorInks,numPresentations); % nonrepeating sequence of values between 1 and 4
% generate random sequence of language, we don't care if this repeats
kLanguage = randi(2,numPresentations); % sequence of 1,2
% generate file paths using folder and color code tables
fileNames = cell(numPresentations,1); % preallocate cell array to hold names
for k = 1:numPresentations
kL = kLanguage(k); % local name to make more readable 1 or 2 for English or Turkish
folder = folderNames{kL};
language = languageCode{kL};
word = colorCode{kL,iColorWord(k)};
ink = colorCode{kL,jColorInk(k)};
file = [language, word, ink,'.JPG']; % for example MSZ.JPG
fileNames{k} = fullfile(folder,file);
end
function r = norepeat(k,n)
% generate n random integers between 1 and k without repetition
% from John D'Errico (Much Thanks!)
% https://www.mathworks.com/matlabcentral/answers/169274-how-can-i-have-random-numbers-with-criteria#answer_164343
r = 1 + mod(cumsum([randi(k),randi(k-1,[1,n-1])]),k);
end
Here is a partial listing of the file names that are generated by a run of the code
'TurkceUyumsuz\TSM.JPG'
'ingilizceUyumlu\MSJ.JPG'
'TurkceUyumsuz\TYM.JPG'
'TurkceUyumsuz\TKY.JPG'
'ingilizceUyumlu\MSC.JPG'
'TurkceUyumsuz\TYY.JPG'
'ingilizceUyumlu\MJC.JPG'
'ingilizceUyumlu\MSS.JPG'
'ingilizceUyumlu\MZC.JPG'
'ingilizceUyumlu\MCZ.JPG'
'ingilizceUyumlu\MSC.JPG'
'TurkceUyumsuz\TKY.JPG'
'TurkceUyumsuz\TMK.JPG'
'TurkceUyumsuz\TKS.JPG'
'TurkceUyumsuz\TYK.JPG'
'ingilizceUyumlu\MJJ.JPG'
'TurkceUyumsuz\TYK.JPG'
'TurkceUyumsuz\TKY.JPG'
'ingilizceUyumlu\MSJ.JPG'
'ingilizceUyumlu\MJC.JPG'
'TurkceUyumsuz\TKS.JPG'
'ingilizceUyumlu\MSZ.JPG'
'TurkceUyumsuz\TSK.JPG'
  5 Comments
Amra Feta
Amra Feta on 15 Dec 2021
Hi @Jon Yes itt is clear the only thing i would need to ask you is where should I insert this code inside my friends already existing code. Here is her code attached.
Jon
Jon on 15 Dec 2021
I already tried to show you in the above edited code, but here it is more explicitly in the attahced file where I have modified your code. I left in some of your code, but commented it out if I thought it was no longer needed. In case I was wrong, and it was needed you can uncomment it. Otherwise you can delete out all of those lines.
Here's an overview:
First you randomly generate the names of the files to be presented and save them to cellarray fileNames.
Then in the experiment you just open up each file in the list and present it.
The function for generating the non-repeating random indices is at the bottom with other helper functions.
I set the numPresentations = 64, this is the total number of images to be shown in an experiment. Not sure if this is correct or not, but you can change it.

Sign in to comment.

More Answers (1)

Amra Feta
Amra Feta on 11 Dec 2021
Hi @Jon,
I will give it one more try since this is for a friend of mine. So what she ttold me for the 8 original files :
English congruent file:
blue color word in blue ink,
green color word in green ink,
red color word in red ink,
yellow color word in yellow ink
Turkish congruent file:
blue color word in blue ink,
green color word in green ink,
red color word in red ink,
yellow color word in yellow ink
The only difference is that the color words are in Turkish
In incongruent files, the ink color and the color words are mismatched
In English incongruent file the color words are in English whereas in Turkish incongruent they are in Turkish
What she wants is to go through the files and to not repeat for example you have blue color word in blue ink then the next one should be either green color word in green ink or an incongruent one to not repeatt the blue color word in blue ink. The only thing I need is how to call i file in matlab and go through it than to not repeat the trial.

Products

Community Treasure Hunt

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

Start Hunting!