How can I randomize the blocks and collect responses from the experiment?

4 views (last 30 days)
Currently I am triying to write a "n-back" experiment with digits between "0" and "9", there should be 8 random blocks that consist random 2-back and 3-back trials. I partially write the experiment but I didnt achieve to create random 8 blocks, also I didnt achieve to collect responses in a text file. Does anyone can help me :(
The code that I wrote is here;
clc;
clear all;
close all;
rng('shuffle');
fid = fopen('Beste.txt', 'wt'); %Open file
fprintf(fid,'%s','FBY ');
fprintf(fid,datestr(now));
fprintf(fid,'%s',' Response');
fprintf(fid,' \n \n');
%fid=fopen(Time, 'a'); % open the file FILENAME in the mode you desire.
ListenChar(2);
HideCursor();
%%%%%%%%%%SETTING UP THE KEYBOARD%%%%%%%%%%%%%%%%%%%%
KbName('UnifyKeyNames');
escapeKey = KbName('ESCAPE');
leftKey = KbName('LeftArrow');
rightKey = KbName('RightArrow');
%%%%%%%%%%%%%%COLORS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
background_color = [0 0 0];
digit_color = [255 255 255];
%%%%%%%%%%OPENNING A SCREEN%%%%%%%%%%%%%%%%%%%%%%%%%%%
Screen('Preference', 'SkipSyncTests', 1);
[window, window_size] = Screen('OpenWindow', 0, background_color, [],32,2);
x_mid = window_size(3)/2;
y_mid = window_size(4)/2;
%%%%%%%%%%%%%%IN BLOCK RANDOMIZATION%%%%%%%%%%%%%%%%%%
block_order = [1];
block_order = Shuffle(block_order);
number_of_blocks = length(block_order);
percent_match = 0.33;
number_of_trials = 24;
actual_trial_counter = 1;
%%%%%%%%%%%%%%%%INSTRUCTION%%%%%%%%%%%%%%%%%%%%%%%%%%
NTrls1=1;
for i=1:NTrls1,
Screen(window,'TextFont','Ariel');
Screen(window,'TextSize', 20 );
DrawFormattedText(window, ['You are going to see series of digits. \n If you see "1-Back" as an instruction please press the \n space bar each time when you see the "one" previous digit. \n If you see "2-Back" as an instruction please press the \n space bar each time when you see the "two" previous digit. \n If you see "3-Back" as an instruction please press the \n space bar each time when you see the "three" previous digit. \n Press any key to start when you are ready!'],'center', 'center', [255 255 255],[],[],[],3 );
Screen('Flip',window);
while ~KbCheck;
end
end
%%%%%%%%%%%%%%%%%%%%2-BACK BLOCK%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for blocks = 1:number_of_blocks
block_type = block_order(blocks);
if block_type == 1
Screen(window,'TextFont','Ariel');
Screen(window,'TextSize', 50 );
DrawFormattedText(window, ['2-Back'],'center', 'center', [255 255 255],[],[],[],3 );
Screen('Flip',window);
end
WaitSecs(5);
memory_bank(1:2) = 0;
for trial_counter = 1:number_of_trials
if block_type == 1
end
if trial_counter <=3
current_number = randi([1 9],1);
should_match = 0;
else
check_for_match = rand(1);
if check_for_match < percent_match
current_number = memory_bank(1);
should_match = 1;
else
while 1
current_number = randi([1 9],1);
if current_number ~= memory_bank(1)
should_match = 0;
break
end
end
end
end
Screen('Flip',window);
random_delay = 0.5 + 2.75;
WaitSecs(random_delay);
KbReleaseWait;
Screen(window,'TextFont','Ariel');
Screen(window,'TextSize', 75);
DrawFormattedText(window, num2str(current_number),'center', 'center', digit_color,[],[],[],2);
Screen('Flip',window);
tic;
response_type = 0;
response_correct = 0;
while 1
[keyIsDown, secs, keyCode] = KbCheck;
if keyCode(escapeKey);
Screen('CloseAll');
end
if keyCode(leftKey) == 1
response_type = 1;
if keyCode (rightKey) == 0
response_correct = -1;
response_type = -1;
end
end
if toc > 1
if response_type == 0 & should_match == 1
response_correct = -1;
end
break;
end
end
if should_match == 1 & response_type == 1
response_correct = 1;
end
student_data(actual_trial_counter,1) = blocks;
student_data(actual_trial_counter,2) = trial_counter;
student_data(actual_trial_counter,3) = current_number;
student_data(actual_trial_counter,4) = memory_bank(2);
student_data(actual_trial_counter,5) = memory_bank(1);
student_data(actual_trial_counter,6) = should_match;
student_data(actual_trial_counter,7) = response_correct;
student_data(actual_trial_counter,8) = response_type;
actual_trial_counter = actual_trial_counter + 1;
memory_bank(1) = memory_bank(2);
memory_bank(2) = current_number;
end
end
Screen('Flip',window);
WaitSecs(1);
%%%%%%%%%%%%%%%%%%%3-BACK BLOCK%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for blocks = 1:number_of_blocks
block_type = block_order(blocks);
if block_type == 1
Screen(window,'TextFont','Ariel');
Screen(window,'TextSize', 50 );
DrawFormattedText(window, ['3-Back'],'center', 'center', [255 255 255],[],[],[],3 );
Screen('Flip',window);
end
WaitSecs(5);
memory_bank(1:2) = 0;
for trial_counter = 1:number_of_trials
if block_type == 1
end
if trial_counter <=3
current_number = randi([1 9],1);
should_match = 0;
else
check_for_match = rand(1);
if check_for_match < percent_match
current_number = memory_bank(1);
should_match = 1;
else
while 1
current_number = randi([1 9],1);
if current_number ~= memory_bank(1)
should_match = 0;
break
end
end
end
end
Screen('Flip',window);
random_delay = 0.5 + 2.75;
WaitSecs(random_delay);
KbReleaseWait;
Screen(window,'TextFont','Ariel');
Screen(window,'TextSize', 75);
DrawFormattedText(window, num2str(current_number),'center', 'center', digit_color,[],[],[],2);
Screen('Flip',window);
tic;
response_type = 0;
response_correct = 0;
while 1
[keyIsDown, secs, keyCode] = KbCheck;
if keyCode(escapeKey);
Screen('CloseAll');
end
if keyCode(leftKey) == 1
response_type = 1;
if keyCode (rightKey) == 0
response_correct = -1;
response_type = -1;
end
end
if toc > 1
if response_type == 0 & should_match == 1
response_correct = -1;
end
break;
end
end
if should_match == 1 & response_type == 1
response_correct = 1;
end
student_data(actual_trial_counter,1) = blocks;
student_data(actual_trial_counter,2) = trial_counter;
student_data(actual_trial_counter,3) = current_number;
student_data(actual_trial_counter,4) = memory_bank(2);
student_data(actual_trial_counter,5) = memory_bank(1);
student_data(actual_trial_counter,6) = should_match;
student_data(actual_trial_counter,7) = response_correct;
student_data(actual_trial_counter,8) = response_type;
actual_trial_counter = actual_trial_counter + 1;
memory_bank(1) = memory_bank(2);
memory_bank(2) = current_number;
end
end
Screen('Flip',window);
WaitSecs(1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%GOOD BYE SCREEN%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Screen(window,'TextFont','Ariel');
Screen(window,'TextSize',30);
DrawFormattedText(window, ['This is the end of our experiment. \n Thank you.'],'center', 'center', digit_color,[],[],[],2);
Screen('Flip',window);
WaitSecs(3);
Screen('CloseAll');
ListenChar(0);
ShowCursor();
fprintf(fid,'%6.0f',duration_comp*1000);
fprintf(fid,'\n');

Answers (0)

Community Treasure Hunt

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

Start Hunting!