-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterruptedSearchAlpha.m
1386 lines (887 loc) · 36.8 KB
/
interruptedSearchAlpha.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function interrupted_search(ID_num, taskCondition)
try
%if isnumeric(ID_num)
% ID_num = num2str(ID_num);
%end
if nargin<2
ID_num = input('Participant ID? ', 's');
taskCondition = input('Task condition (1, 2, 3, 4 or 5)? ');
end
tstamp = clock;
savefile = fullfile(pwd, 'Results', [sprintf('interruptedSearch-%02d-%02d-%02d-%02d-%02d-task-%d-participant-%s', tstamp(1), tstamp(2), tstamp(3), tstamp(4), tstamp(5), taskCondition), ID_num, '.txt']);
savefilenodis = fullfile(pwd, 'Results', [sprintf('interruptedSearch-%02d-%02d-%02d-%02d-%02d-task-%d-participant-%s', tstamp(1), tstamp(2), tstamp(3), tstamp(4), tstamp(5), taskCondition), ID_num, '_NoDis.txt']);
savefilemat = fullfile(pwd, 'Results', [sprintf('interruptedSearch-%02d-%02d-%02d-%02d-%02d-task-%d-participant-%s', tstamp(1), tstamp(2), tstamp(3), tstamp(4), tstamp(5), taskCondition), ID_num, '.mat']);
fileID = fopen(savefile,'wt+');
noDisfileID = fopen(savefilenodis,'wt+');
fprintf(fileID,'Block\tTrial\tCR\tResponse\tPosition\tDirection\tSearchSize\tTime\tEpoch\tSwitch_type\n');
fprintf(noDisfileID,'Trial\tCR\tResponse\tPosition\tDirection\tTime\tEpoch\n');
%fprintf(fileID,'%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n', blockLoop, loop, keyresponse, targetInfo, targetPos, targetDir, num_items, reactionTime, displayLoops);
% Screen('Preference', 'SkipSyncTests', 1);
%% Experiment Variables.
% save the script content
nosearchdata = struct;
data = struct;
% data.scripts = savescripts;
data.task = taskCondition;
data.ID = ID_num;
scr_background = 127.5;
scr_no = 0;
%white = WhiteIndex(scr_no);
white = 255;
black = BlackIndex(scr_no);
scr_dimensions = Screen('Rect', scr_no);
xcen = scr_dimensions(3)/2;
ycen = scr_dimensions(4)/2;
xfull = scr_dimensions(3);
yfull = scr_dimensions(4);
% Set up Keyboard
WaitSecs(1);
KbName('UnifyKeyNames');
left_key = KbName('LeftArrow');
right_key = KbName('RightArrow');
down_key = KbName('DownArrow');
esc_key = KbName('Escape');
space_key = KbName('space');
yes_key = KbName('y');
no_key = KbName('n');
keyList = zeros(1, 256);
keyList([yes_key, no_key, left_key, right_key, down_key, esc_key, space_key]) = 1;
KbQueueCreate([], keyList);
%clear keyList;
RestrictKeysForKbCheck([]);
% Sound
InitializePsychSound;
pa = PsychPortAudio('Open', [], [], [], [], [], 256);
bp400 = PsychPortAudio('CreateBuffer', pa, [MakeBeep(400, 0.2); MakeBeep(400, 0.2)]);
PsychPortAudio('FillBuffer', pa, bp400);
% Open Window
scr = Screen('OpenWindow', scr_no, scr_background);
frameRate = 1/Screen('FrameRate', scr);
HideCursor;
Screen('BlendFunction', scr, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Screen('TextFont', scr, 'Ariel');
Screen('TextSize', scr, 40);
% Experiment Procedure
nBlocks = 10;
nTrials = 45;
Priority(1);
%% Instructions
% if taskCondition == 1 || taskCondition == 3 || taskCondition == 4
% DrawFormattedText(scr, 'Welcome. This experiment is a visual search experiment.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'You will have to find a "T" in a range of "L"s.\n\nYou will then have to identify the colour of that "T".', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'The T and the Ls can be red or blue.\n\nIf the T is red, you will have to press the "Right" Arrow Key,\n\nand if it is blue, you will have to press the "Left" Arrow Key.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'The display will only be shown intermittently, with blank intervals in between.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'You have a total of 8 seconds to find the target in each trial.\n\nAfter each trial, you will be told if you got it correct or not.\n\nIf not, a beeping sound will indicate the error.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% [~, ny] = DrawFormattedText(scr, 'This is an example target.\n\nHere, the correct button would be the "Left" Arrow Key.', 'center', 'center', 0);
% display_object(8, 0, [xcen, ny + 100])
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% % Next, a full display
% DrawFormattedText(scr, 'This is an example of the full display. When you find the target,\n\npress "Right" if it is red,\n\nor "Left" if it is blue.', 'center', 20, 0);
% baseRect = [0 0 420 420];
% centeredRect = CenterRectOnPointd(baseRect, xcen, ycen);
% Screen('FillRect', scr, white, centeredRect);
% [demoElements] = create_grid(3, 2, 2, 16, 1, 999);
% for demoObject = 1:16
% demotype = cell2mat(demoElements(demoObject,1));
% democolour = cell2mat(demoElements(demoObject,2));
% demo_x = cell2mat(demoElements(demoObject,3));
% demo_y = cell2mat(demoElements(demoObject,4));
% display_object(demotype, democolour, [demo_x demo_y]);
% end
% Screen('Flip', scr);
% while 1
% [~, demokeys, ~] = KbStrokeWait;
% if demokeys(right_key)
% break
% else
% PsychPortAudio('Start', pa);
% end
% end
%
% DrawFormattedText(scr, 'Great. The aim is to complete the task\n\nas quickly as possible, but also get each answer right.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to start the experiment.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'If you have any questions\n\nask the examiner now.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to start the experiment.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
%
% elseif taskCondition == 2
%
% DrawFormattedText(scr, 'Welcome. This experiment is a visual search experiment.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'You will have to find a "T" in a range of either blue or red "L"s.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'You will alternately be shown red and blue letters.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'If the T is in the red letters, you will have to press the "Right" key.\n\nIf it is in the blue letters, you will have to press the "Left" Key.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'On some trials, there will be no T - only Ls.\n\nIn that case, you will have to press the "Down" button.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'You have a total of 8 seconds to find the target in each trial.\n\nAfter each trial, you will be told if you got it correct or not.\n\nIf not, a beeping sound will indicate the error.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% [~, ny] = DrawFormattedText(scr, 'This is an example target.\n\nHere, the correct button would be the "Left" Arrow Key.', 'center', 'center', 0);
% display_object(8, 0, [xcen, ny + 100])
% DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'The aim is to complete the task\n\nas quickly as possible, but also get each answer right.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to start the experiment.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% DrawFormattedText(scr, 'If you have any questions\n\nask the examiner now.', 'center', 'center', 0);
% DrawFormattedText(scr, 'Press "Space" to start the experiment.', 'center', yfull-80, 0);
% Screen('Flip', scr); KbStrokeWait;
% end
%% Distractor free trials
DrawFormattedText(scr, 'Before starting the main experiment, you will do a short warm up task. \n\n\n\n This will be similar to the main task. \n\n However the T will appear on its own without any Ls.', 'center', 'center', 0);
DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
Screen('Flip', scr); KbStrokeWait;
DrawFormattedText(scr, 'Press the "Right" key if the T is red or the "Left" key if the T is blue.', 'center', 'center', 0);
DrawFormattedText(scr, 'Press "Space" to continue.', 'center', yfull-80, 0);
Screen('Flip', scr); KbStrokeWait;
DrawFormattedText(scr, 'When you are ready to start the warm up, press the "Space" key.', 'center', 'center', 0);
Screen('Flip', scr); KbStrokeWait;
blockLoop = '88';
% taskA(30, 3, 2);
%% Practice trials
% training_demo(taskCondition);
DrawFormattedText(scr, 'If you have any questions\n\nask the examiner now.', 'center', 'center', 0);
DrawFormattedText(scr, 'Press "Space" to start the experiment.', 'center', yfull-80, 0);
Screen('Flip', scr); KbStrokeWait;
%% Main body
%create_display;
%display_object(1, [xcen+100 ycen-400]);
count_text = 'The task will start in';
countdown_pause(5, count_text);
if taskCondition == 2
for blockLoop = 1:nBlocks
taskB(nTrials, 1);
if blockLoop ~= 10
count_text = sprintf('Block %d of 10 is now complete. Pause for', blockLoop);
countdown_pause(30, count_text);
end
end
elseif taskCondition == 3
for blockLoop = 1:nBlocks
taskA(nTrials, 2, 1);
if blockLoop ~= 10
count_text = sprintf('Block %d of 10 is now complete. Pause for', blockLoop);
countdown_pause(30, count_text);
end
end
elseif taskCondition == 4
for blockLoop = 1:nBlocks
taskA(nTrials, 4, 1);
if blockLoop ~= 10
count_text = sprintf('Block %d of 10 is now complete. Pause for', blockLoop);
countdown_pause(30, count_text);
end
end
elseif taskCondition == 5
for blockLoop = 1:nBlocks
taskA(nTrials, 5, 1);
if blockLoop ~= 10
count_text = sprintf('Block %d of 10 is now complete. Pause for', blockLoop);
countdown_pause(30, count_text);
end
end
else
for blockLoop = 1:nBlocks
taskA(nTrials, 1, 1);
if blockLoop ~= 10
count_text = sprintf('Block %d of 10 is now complete. Pause for', blockLoop);
countdown_pause(30, count_text);
end
end
end
count_text = 'The experiment will finish in';
countdown_pause(10, count_text);
%% Shutdown
fclose(fileID);
fclose(noDisfileID);
text_string = 'Thank you!';
DrawFormattedText(scr, text_string, 'center', 'center', 0);
Screen('Flip', scr);
WaitSecs(2);
KbQueueStop;
PsychPortAudio('Close');
sca;
save(savefilemat, 'data', 'nosearchdata');
Priority(0);
disp('All done!');
catch errMessage
KbQueueStop;
sca;
%savefilemat = fullfile(pwd, 'Results', 'Errors', [sprintf('interruptedSearch-%02d-%02d-%02d-%02d-%02d-task-%d-participant-%s', tstamp(1), tstamp(2), tstamp(3), tstamp(4), tstamp(5), taskCondition), ID_num, '_error.mat']);
% save(savefilemat, 'data', 'nosearchdata');
rethrow(errMessage);
end
%% Subfunctions
%% Task A
function taskA(trialsNum, altSettings, writeFile)
shortTrials = randsample(1:trialsNum, (trialsNum / 2));
redTargetTrials = randsample(1:trialsNum, (trialsNum / 2));
smallTrials = randsample(1:trialsNum, (trialsNum / 2));
trialCounter = 0;
seqLength = 12;
seqCount = 0;
colour_switch = 0;
% Create trial list for option 5
if altSettings == 5
uniquetrials = ceil(trialsNum/3);
temp_trials_vector = repmat(1:3, [uniquetrials 1]);
temp_trials_vector = temp_trials_vector(:)';
trials_vector = temp_trials_vector(randperm(length(temp_trials_vector)));
end
for loop = 1:trialsNum
if seqCount == seqLength
seqCount = 0;
end
trialCounter = trialCounter + 1;
seqCount = seqCount + 1;
if isempty(find(smallTrials == trialCounter))
num_items = 32;
else
num_items = 16;
end
if isempty(find(redTargetTrials == trialCounter))
targetColour = 1;
else
targetColour = 2;
end
fixation_cross;
WaitSecs(0.5);
if altSettings == 2
if isempty(find(shortTrials == trialCounter))
searchDuration = 0.5;
blankDuration = 1.6;
else
searchDuration = 0.1;
blankDuration = 2;
end
else
searchDuration = 0.1;
blankDuration = 0.9;
end
if altSettings == 3
distractorOn = 2;
else
distractorOn = 1;
end
if altSettings == 4
determSeq = seqCount;
else
determSeq = 999;
end
[objectElements, targetInfo, targetPos, targetDir] = create_grid(3, 2, targetColour, num_items, distractorOn, determSeq);
blankStart = GetSecs-blankDuration + 0.05;
reactionTime = 0;
for displayLoops = 1:8
baseRect = [0 0 420 420];
centeredRect = CenterRectOnPointd(baseRect, xcen, ycen);
Screen('FillRect', scr, white, centeredRect);
objectLoops = size(objectElements,1);
for displayObject = 1:objectLoops
objecttype = cell2mat(objectElements(displayObject,1));
objectcolour = cell2mat(objectElements(displayObject,2));
x_final = cell2mat(objectElements(displayObject,3));
y_final = cell2mat(objectElements(displayObject,4));
if altSettings == 5
if trials_vector(loop) == 2
Switch_type = 'Target';
elseif trials_vector(loop) == 3
Switch_type = 'All';
else
Switch_type = 'None';
end
if mod(displayLoops, 2) == 0
colour_switch = 0;
display_object(objecttype, objectcolour, [x_final y_final]);
else
if trials_vector(loop) == 2
colour_switch = 1;
if objecttype < 9 && objecttype > 4
if objectcolour == 1
newobjectcolour = 0;
else
newobjectcolour = 1;
end
display_object(objecttype, newobjectcolour, [x_final y_final]);
else
display_object(objecttype, objectcolour, [x_final y_final]);
end
elseif trials_vector(loop) == 3
if objectcolour == 1
newobjectcolour = 0;
else
newobjectcolour = 1;
end
colour_switch = 1;
display_object(objecttype, newobjectcolour, [x_final y_final]);
else
colour_switch = 0;
display_object(objecttype, objectcolour, [x_final y_final]);
end
end
else
colour_switch = 0;
Switch_type = 'None'
display_object(objecttype, objectcolour, [x_final y_final]);
end
end
startSecs = Screen('Flip', scr, blankStart + blankDuration - frameRate/2);
if displayLoops == 1
trialStart = startSecs;
end
keyresponse = 'null';
%startSecs = GetSecs;
KbQueueFlush();
KbQueueStart();
WaitSecs(searchDuration-0.05);
[ ~, first_press] = KbQueueCheck;
timeSecs = min(first_press(first_press~=0));
if first_press(esc_key)
error('You interrupted the script by pressing Escape after exposure');
elseif ~isempty(timeSecs) && first_press(left_key)==timeSecs
keyresponse = 'left';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(right_key)==timeSecs
keyresponse = 'right';
reactionTime = timeSecs - trialStart;
break;
end
baseRect = [0 0 420 420];
centeredRect = CenterRectOnPointd(baseRect, xcen, ycen);
Screen('FillRect', scr, white, centeredRect);
blankStart = Screen('Flip', scr, startSecs+searchDuration - frameRate/2);
WaitSecs(blankDuration - 0.05);
[ ~, first_press] = KbQueueCheck;
timeSecs = min(first_press(first_press~=0));
if first_press(esc_key)
error('You interrupted the script by pressing Escape after exposure');
elseif ~isempty(timeSecs) && first_press(left_key)==timeSecs
keyresponse = 'left';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(right_key)==timeSecs
keyresponse = 'right';
reactionTime = timeSecs - trialStart;
break;
end
KbQueueStop;
%KbWait;
end
Screen('Flip', scr);
if colour_switch == 1
if strcmp(targetInfo, 'Red')
targetInfo = 'Blue'
else
targetInfo = 'Red'
end
end
% Store data in txt file
if writeFile == 3
disp('Not writing to output file!');
elseif writeFile == 2
fprintf(noDisfileID,'%d\t%s\t%s\t%d\t%d\t%d\t%d\n', loop, keyresponse, targetInfo, targetPos, targetDir, reactionTime, displayLoops);
else
fprintf(fileID,'%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%s\n', blockLoop, loop, keyresponse, targetInfo, targetPos, targetDir, num_items, reactionTime, displayLoops, Switch_type);
end
WaitSecs(0.2);
% give participant feedback
if (strcmp(targetInfo, 'Red') && strcmp(keyresponse, 'right')) || (strcmp(targetInfo, 'Blue') && strcmp(keyresponse, 'left'))
DrawFormattedText(scr, 'Correct.', 'center', 'center');
elseif (strcmp(targetInfo, 'Red') && strcmp(keyresponse, 'left')) || (strcmp(targetInfo, 'Blue') && strcmp(keyresponse, 'right'))
DrawFormattedText(scr, 'Wrong.', 'center', 'center');
PsychPortAudio('Start', pa);
elseif strcmp(keyresponse, 'null')
DrawFormattedText(scr, 'Too slow.', 'center', 'center');
PsychPortAudio('Start', pa);
end
% Store data in matlab structure
if isnumeric(blockLoop)
data.n = (blockLoop-1)*trialsNum + loop;
data.keyresponse{data.n} = keyresponse;
data.rt(data.n) = reactionTime;
data.correct(data.n) = (strcmp(targetInfo, 'Red') && strcmp(keyresponse, 'right')) || (strcmp(targetInfo, 'Blue') && strcmp(keyresponse, 'left'));
data.targetcolor{data.n} = targetInfo;
data.targetpos{data.n} = targetPos;
data.displaysize(data.n) = num_items;
data.epoch(data.n) = displayLoops;
elseif strcmp(blockLoop, '88')
nosearchdata.n = loop;
nosearchdata.keyresponse{nosearchdata.n} = keyresponse;
nosearchdata.rt(nosearchdata.n) = reactionTime;
nosearchdata.correct(nosearchdata.n) = (strcmp(targetInfo, 'Red') && strcmp(keyresponse, 'right')) || (strcmp(targetInfo, 'Blue') && strcmp(keyresponse, 'left'));
nosearchdata.targetcolor{nosearchdata.n} = targetInfo;
nosearchdata.targetpos{nosearchdata.n} = targetPos;
nosearchdata.displaysize(nosearchdata.n) = num_items;
nosearchdata.epoch(nosearchdata.n) = displayLoops;
end
Screen('Flip', scr);
WaitSecs(0.8);
end
end
%% Task B
function taskB(trialsNum, writeFile)
trialCounter = 0;
downTrialsNumber = trialsNum / 5;
redTrialsNumber = (trialsNum - downTrialsNumber) / 2;
downTrials = randsample(trialsNum, downTrialsNumber);
emptyTrials = setdiff(1:trialsNum, downTrials);
redTrials = randsample(emptyTrials, redTrialsNumber);
smallTrials = randsample(1:trialsNum, (trialsNum / 2));
for loop = 1:trialsNum
trialCounter = trialCounter + 1;
if isempty(find(smallTrials == trialCounter))
num_items = 32;
else
num_items = 16;
end
fixation_cross;
WaitSecs(0.5);
if ~isempty(find(downTrials == trialCounter))
targetOptionRed = 1;
targetOptionBlue = 1;
elseif ~isempty(find(redTrials == trialCounter))
targetOptionRed = 2;
targetOptionBlue = 1;
else
targetOptionRed = 1;
targetOptionBlue = 2;
end
[objectElementsRed, targetInfoRed, targetPos, targetDir] = create_grid(1, targetOptionRed, 2, num_items, 1, 999);
[objectElementsBlue, targetInfoBlue, targetPos, targetDir] = create_grid(2, targetOptionBlue, 1, num_items, 1, 999);
targetInfo = strcat(targetInfoRed, targetInfoBlue);
display = struct([]);
for iDisplay = 1:8
display(iDisplay).xy = zeros(2, 4*size(objectElementsRed, 1));
for object = 1:size(objectElementsRed, 1);
objecttype = cell2mat(objectElementsRed(object,1));
objectcolour = cell2mat(objectElementsRed(object,2));
mid_coords = [objectElementsRed{object,3}, objectElementsRed{object,4}];
display(iDisplay).xy(1:2, (4*object-3):(4*object)) = make_object_xy(objecttype, objectcolour, mid_coords);
if objectcolour == 0
display(iDisplay).colors(1:3, (4*object-3):(4*object)) = repmat([0;0;255], 1, 4);
else
display(iDisplay).colors(1:3, (4*object-3):(4*object)) = repmat([255;0;0], 1, 4);
end
end
end
searchDuration = 0.1;
blankDuration = 0.95;
blankStartB = GetSecs-blankDuration+0.02;
reactionTime = 0;
for displayLoops = 1:8
baseRect = [0 0 420 420];
centeredRect = CenterRectOnPointd(baseRect, xcen, ycen);
Screen('FillRect', scr, white, centeredRect);
Screen('DrawLines', scr, display(displayLoops).xy, 4, display(displayLoops).colors, [], 2);
startSecsA = Screen('Flip', scr, blankStartB + blankDuration - frameRate/2);
if displayLoops == 1
trialStart = startSecsA;
end
keyresponse = 'null';
%startSecs = GetSecs;
KbQueueFlush();
KbQueueStart();
WaitSecs(searchDuration-0.02);
[ ~, first_press] = KbQueueCheck;
timeSecs = min(first_press(first_press~=0));
if first_press(esc_key)
error('You interrupted the script by pressing Escape after exposure');
elseif ~isempty(timeSecs) && first_press(left_key)==timeSecs
keyresponse = 'left';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(right_key)==timeSecs
keyresponse = 'right';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(down_key)==timeSecs
keyresponse = 'down';
reactionTime = timeSecs - trialStart;
break;
end
baseRect = [0 0 420 420];
centeredRect = CenterRectOnPointd(baseRect, xcen, ycen);
Screen('FillRect', scr, white, centeredRect);
blankStartA = Screen('Flip', scr, startSecsA+searchDuration - frameRate/2);
WaitSecs(blankDuration - 0.02);
[ ~, first_press] = KbQueueCheck;
timeSecs = min(first_press(first_press~=0));
if first_press(esc_key)
error('You interrupted the script by pressing Escape after exposure');
elseif ~isempty(timeSecs) && first_press(left_key)==timeSecs
keyresponse = 'left';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(right_key)==timeSecs
keyresponse = 'right';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(down_key)==timeSecs
keyresponse = 'down';
reactionTime = timeSecs - trialStart;
break;
end
Screen('FillRect', scr, white, centeredRect);
objectLoops = size(objectElementsBlue,1);
for displayObject = 1:objectLoops
objecttype = cell2mat(objectElementsBlue(displayObject,1));
objectcolour = cell2mat(objectElementsBlue(displayObject,2));
x_final = cell2mat(objectElementsBlue(displayObject,3));
y_final = cell2mat(objectElementsBlue(displayObject,4));
display_object(objecttype, objectcolour, [x_final y_final]);
end
startSecsB = Screen('Flip', scr, blankStartA + blankDuration - frameRate/2);
WaitSecs(searchDuration-0.02);
[ ~, first_press] = KbQueueCheck;
timeSecs = min(first_press(first_press~=0));
if first_press(esc_key)
error('You interrupted the script by pressing Escape after exposure');
elseif ~isempty(timeSecs) && first_press(left_key)==timeSecs
keyresponse = 'left';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(right_key)==timeSecs
keyresponse = 'right';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(down_key)==timeSecs
keyresponse = 'down';
reactionTime = timeSecs - trialStart;
break;
end
baseRect = [0 0 420 420];
centeredRect = CenterRectOnPointd(baseRect, xcen, ycen);
Screen('FillRect', scr, white, centeredRect);
blankStartB = Screen('Flip', scr, startSecsB + searchDuration - frameRate/2);
WaitSecs(blankDuration-0.02);
[ ~, first_press] = KbQueueCheck;
timeSecs = min(first_press(first_press~=0));
if first_press(esc_key)
error('You interrupted the script by pressing Escape after exposure');
elseif ~isempty(timeSecs) && first_press(left_key)==timeSecs
keyresponse = 'left';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(right_key)==timeSecs
keyresponse = 'right';
reactionTime = timeSecs - trialStart;
break;
elseif ~isempty(timeSecs) && first_press(down_key)==timeSecs
keyresponse = 'down';
reactionTime = timeSecs - trialStart;
break;
end
KbQueueStop;
%KbWait;
end
if isempty(targetInfo)
targetInfo = 'Blank';
end
if writeFile == 2
disp('Not writing to output file!');
else
fprintf(fileID,'%d\t%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n', blockLoop, loop, keyresponse, targetInfo, targetPos, targetDir, num_items, reactionTime, displayLoops);
end
WaitSecs(0.2);
% give participant feedback
if (strcmp(targetInfo, 'Red') && strcmp(keyresponse, 'right')) || (strcmp(targetInfo, 'Blue') && strcmp(keyresponse, 'left')) || (strcmp(targetInfo, 'Blank') && strcmp(keyresponse, 'down'))
DrawFormattedText(scr, 'Correct.', 'center', 'center');
elseif (strcmp(targetInfo, 'Red') && ~strcmp(keyresponse, 'right')) || (strcmp(targetInfo, 'Blue') && ~strcmp(keyresponse, 'left')) || (strcmp(targetInfo, 'Blank') && ~strcmp(keyresponse, 'down'))
DrawFormattedText(scr, 'Wrong.', 'center', 'center');
PsychPortAudio('Start', pa);
elseif strcmp(keyresponse, 'null')
DrawFormattedText(scr, 'Too slow.', 'center', 'center');
PsychPortAudio('Start', pa);
end
Screen('Flip', scr);
WaitSecs(0.8);
% Store data in matlab structure
if isnumeric(blockLoop)
data.n = (blockLoop-1)*trialsNum + loop;
data.keyresponse{data.n} = keyresponse;
data.rt(data.n) = reactionTime;
data.correct(data.n) = (strcmp(targetInfo, 'Red') && strcmp(keyresponse, 'right')) || (strcmp(targetInfo, 'Blue') && strcmp(keyresponse, 'left')) || (strcmp(targetInfo, 'Blank') && strcmp(keyresponse, 'down'));
data.targetcolor{data.n} = targetInfo;
data.targetpos{data.n} = targetPos;
data.displaysize(data.n) = num_items;
data.epoch(data.n) = displayLoops;
end
end
end
%% other subfunctions
function fixation_cross
fixCrossDimPix = 10;
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords = [xCoords; yCoords];
lineWidthPix = 4;
Screen('DrawLines', scr, allCoords,lineWidthPix, black, [xcen ycen], 2);
Screen('Flip', scr);
end
function display_object(objecttype, objectcolour, mid_coords)
%global scr_no xcen ycen scr;
%black = BlackIndex(scr_no);
fixCrossDimPix = 10;
if objecttype == 9
xCoords = [0 0 0 0];
yCoords = [0 0 0 0];
elseif objecttype == 8
xCoords = [-fixCrossDimPix -fixCrossDimPix -fixCrossDimPix fixCrossDimPix];
yCoords = [fixCrossDimPix -fixCrossDimPix 0 0];
elseif objecttype == 7
xCoords = [fixCrossDimPix fixCrossDimPix -fixCrossDimPix fixCrossDimPix];
yCoords = [fixCrossDimPix -fixCrossDimPix 0 0];
elseif objecttype == 6
xCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
yCoords = [fixCrossDimPix -fixCrossDimPix fixCrossDimPix fixCrossDimPix];
elseif objecttype == 5
xCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
yCoords = [fixCrossDimPix -fixCrossDimPix -fixCrossDimPix -fixCrossDimPix];
elseif objecttype == 4
xCoords = [-fixCrossDimPix fixCrossDimPix+2 fixCrossDimPix fixCrossDimPix];
yCoords = [-fixCrossDimPix -fixCrossDimPix -fixCrossDimPix fixCrossDimPix+2];
elseif objecttype == 3
xCoords = [-fixCrossDimPix fixCrossDimPix+2 fixCrossDimPix fixCrossDimPix];
yCoords = [fixCrossDimPix fixCrossDimPix -fixCrossDimPix fixCrossDimPix+2];
elseif objecttype == 2
xCoords = [-(fixCrossDimPix+2) fixCrossDimPix -fixCrossDimPix -fixCrossDimPix];
yCoords = [-fixCrossDimPix -fixCrossDimPix -fixCrossDimPix fixCrossDimPix+2];
elseif objecttype == 1
xCoords = [-fixCrossDimPix fixCrossDimPix+2 -fixCrossDimPix -fixCrossDimPix];