-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
203 lines (147 loc) · 5.85 KB
/
main.m
File metadata and controls
203 lines (147 loc) · 5.85 KB
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
% Clear all the previous stuff
clear;
clc;
if ~ismac
close all;
clear Screen;
end
% make sure we got access to all the required functions and inputs
initEnv();
% set and load all the parameters to run the experiment
cfg = setParameters;
cfg = userInputs(cfg);
cfg.startWith = input('Start with stimuli set A or B: ', 's');
% set defaults in case no the proper answer
if ~ismember(cfg.startWith, {'A', 'B'})
cfg.startWith = 'A';
end
talkToMe(cfg, sprintf('\nLocalizer Experiment: start with stimuli subset: %s\n\n', ...
cfg.startWith));
cfg = createFilename(cfg);
% Prepare for the output logfiles with all
logFile = struct('extraColumns', {cfg.extraColumns}, ...
'isStim', false);
logFile = saveEventsFile('init', cfg, logFile);
logFile = saveEventsFile('open', cfg, logFile);
%% SET THE STIMULI/CATEGORY
talkToMe(cfg, '\nLoad stimuli:');
imgStruct = loadImages();
imgNames = fieldnames(imgStruct);
setUpRand();
allCatRep = shuffleBlocks(cfg);
%% Experiment
% Safety loop: close the screen if code crashes
try
%% Init the experiment
cfg = initPTB(cfg);
[cfg, imgStruct] = postInitializationSetup(cfg, imgStruct);
unfold(cfg);
% prepare the KbQueue to collect responses
getResponse('init', cfg.keyboard.responseBox, cfg);
standByScreen(cfg);
talkToMe(cfg, '\nWAITING FOR TRIGGER (Instructions displayed on the screen)\n\n');
waitForTrigger(cfg);
drawFixation(cfg);
cfg.experimentStart = Screen('Flip', cfg.screen.win);
getResponse('start', cfg.keyboard.responseBox);
waitFor(cfg, cfg.timing.onsetDelay);
for iBlock = 1:length(allCatRep)
% get the names of the images to display from the block type
idx = cellfun(@(x) strcmp(x(1:2), allCatRep{iBlock}), imgNames);
stimuli = imgNames(idx);
stimuli = Shuffle(stimuli);
nbStim = length(stimuli);
if cfg.debug.do
nbStim = 10;
end
% Set the target for this block
% it will randomly pick one of these
possibleNumberOfTargets = [0 1 2];
nbTargets = possibleNumberOfTargets(randperm(length(possibleNumberOfTargets), 1));
% sort randomly the stimuli in the block
[~, idx] = sort(rand(1, nbStim));
% select the position of the target(s)
positionTarget = sort(idx(1:nbTargets));
talkToMe(cfg, sprintf('\nNumber of targets in coming trial: %i\n\n', nbTargets));
for iTrial = 1:nbStim
% Check for experiment abortion from operator
checkAbort(cfg, cfg.keyboard.keyboard);
thisTrial.isStim = logFile.isStim;
thisTrial.fileID = logFile.fileID;
thisTrial.extraColumns = logFile.extraColumns;
thisTrial.block_nb = iBlock;
thisTrial.trial_nb = iTrial;
thisTrial.target = false;
thisTrial.key_name = 'n/a';
thisTrial.stim_file = [stimuli{iTrial} '.tif'];
switch stimuli{iTrial}(1)
case 'F'
trial_type = 'face';
case 'H'
trial_type = 'house';
case 'W'
trial_type = 'word';
end
thisTrial.trial_type = trial_type;
thisTrial.texture = imgStruct.(stimuli{iTrial}).texture;
thisTrial = playTrial(cfg, thisTrial);
if iTrial == 1
blockStart = thisTrial.onset;
end
% if this is a target
% repeat the same stimulus
if sum(iTrial == positionTarget) == 1
thisTrial.target = true;
thisTrial.trial_type = 'target';
thisTrial = playTrial(cfg, thisTrial);
end
%% Collect and saves the responses
responseEvents = getResponse('check', cfg.keyboard.responseBox, cfg);
if isfield(responseEvents(1), 'onset') && ~isempty(responseEvents(1).onset)
for iResp = 1:size(responseEvents, 1)
responseEvents(iResp).onset = ...
responseEvents(iResp).onset - cfg.experimentStart;
responseEvents(iResp).key_name = responseEvents(iResp).keyName;
end
responseEvents(1).isStim = false;
responseEvents(1).fileID = logFile.fileID;
responseEvents(1).extraColumns = logFile.extraColumns;
saveEventsFile('save', cfg, responseEvents);
end
end
blockEnd = GetSecs();
blockDuration = (blockEnd - cfg.experimentStart) - blockStart;
talkToMe(cfg, sprintf('\n\nTiming - Block duration: %0.3f seconds\n\n', blockDuration));
WaitSecs(cfg.timing.IBI);
end
loopDuration = (GetSecs() - cfg.experimentStart);
loopDurationMin = floor(loopDuration / 60);
loopDurationSec = mod(loopDuration, 60);
talkToMe(cfg, sprintf('\n\nTiming - Run lasted %i minutes %0.3f seconds\n\n', ...
loopDurationMin, ...
loopDurationSec));
getResponse('stop', cfg.keyboard.responseBox);
getResponse('release', cfg.keyboard.responseBox);
saveEventsFile('close', cfg, logFile);
createJson(cfg, cfg);
% Pad the runtime to make sure all runs have same duraton
% (due to random nb of targets)
endDelay = cfg.timing.runDuration - loopDuration;
if ~cfg.debug.do
WaitSecs(endDelay);
end
cfg = getExperimentEnd(cfg);
farewellScreen(cfg, 'Fin de l''experience :)\nMERCI !');
cleanUp();
cpp_ptb('uninit');
cpp_bids('uninit');
pth = fileparts(mfilename('fullpath'));
rmpath(fullfile(pth, 'supporting_functions'));
catch
cleanUp();
cpp_ptb('uninit');
cpp_bids('uninit');
pth = fileparts(mfilename('fullpath'));
rmpath(fullfile(pth, 'supporting_functions'));
psychrethrow(psychlasterror);
end