-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFunctionalLocalizer_main.m
385 lines (249 loc) · 9.42 KB
/
FunctionalLocalizer_main.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
function FunctionalLocalizer_main(hObject, ~)
if nargin == 0
FunctionalLocalizer_GUI;
fprintf('\n')
fprintf('Use %s to start the GUI.','FunctionalLocalizer_GUI.m');
fprintf('\n')
return
end
handles = guidata(hObject); % retrieve GUI data
clc
sca
% Initialize the main structure
DataStruct = struct;
DataStruct.TimeStamp = datestr(now, 'yyyy-mm-dd HH:MM'); % readable
DataStruct.TimeStampFile = datestr(now, 30); % to sort automatically by time of creation
%% Task selection
switch get(hObject,'Tag')
case 'pushbutton_EyelinkCalibration'
Task = 'EyelinkCalibration';
case 'pushbutton_Calibration'
Task = 'Calibration';
case 'pushbutton_Instructions'
Task = 'Instructions';
case 'pushbutton_Session'
Task = 'Session';
otherwise
error('FunctionalLocalizer:TaskSelection','Error in Task selection')
end
DataStruct.Task = Task;
%% Environement selection
switch get(get(handles.uipanel_Environement,'SelectedObject'),'Tag')
case 'radiobutton_MRI'
Environement = 'MRI';
case 'radiobutton_Training'
Environement = 'Training';
otherwise
warning('FunctionalLocalizer:ModeSelection','Error in Environement selection')
end
DataStruct.Environement = Environement;
%% Save mode selection
switch get(get(handles.uipanel_SaveMode,'SelectedObject'),'Tag')
case 'radiobutton_SaveData'
SaveMode = 'SaveData';
case 'radiobutton_NoSave'
SaveMode = 'NoSave';
otherwise
warning('FunctionalLocalizer:SaveSelection','Error in SaveMode selection')
end
DataStruct.SaveMode = SaveMode;
%% Mode selection
switch get(get(handles.uipanel_OperationMode,'SelectedObject'),'Tag')
case 'radiobutton_Acquisition'
OperationMode = 'Acquisition';
case 'radiobutton_FastDebug'
OperationMode = 'FastDebug';
case 'radiobutton_RealisticDebug'
OperationMode = 'RealisticDebug';
otherwise
warning('FunctionalLocalizer:ModeSelection','Error in Mode selection')
end
DataStruct.OperationMode = OperationMode;
%% Record video ?
switch get(get(handles.uipanel_RecordVideo,'SelectedObject'),'Tag')
case 'radiobutton_RecordOn'
RecordVideo = 'On';
VideoName = [ get(handles.edit_RecordName,'String') '.mov'];
DataStruct.VideoName = VideoName;
case 'radiobutton_RecordOff'
RecordVideo = 'Off';
otherwise
warning('FunctionalLocalizer:RecordVideo','Error in Record Video')
end
DataStruct.RecordVideo = RecordVideo;
%% Subject ID & Run number
SubjectID = get(handles.edit_SubjectID,'String');
if length(SubjectID) ~= 4
error('FunctionalLocalizer:SubjectIDLength','\n SubjectID must be 4 char \n')
end
% Prepare path
DataPath = [fileparts(pwd) filesep 'data' filesep SubjectID filesep];
DataPathNoRun = sprintf('%s_%s_%s_', SubjectID, Task, Environement);
% Fetch content of the directory
dirContent = dir(DataPath);
% Is there file of the previous run ?
previousRun = nan(length(dirContent),1);
for f = 1 : length(dirContent)
split = regexp(dirContent(f).name,DataPathNoRun,'split');
if length(split) == 2 && str2double(split{2}(1)) % yes there is a file
previousRun(f) = str2double(split{2}(1)); % save the previous run numbers
else % no file found
previousRun(f) = 0; % affect zero
end
end
LastRunNumber = max(previousRun);
% If no previous run, LastRunNumber is 0
if isempty(LastRunNumber)
LastRunNumber = 0;
end
RunNumber = num2str(LastRunNumber + 1);
DataFile = sprintf('%s%s_%s_%s_%s_%s', DataPath, DataStruct.TimeStampFile, SubjectID, Task, Environement, RunNumber );
DataStruct.SubjectID = SubjectID;
DataStruct.RunNumber = RunNumber;
DataStruct.DataPath = DataPath;
DataStruct.DataFile = DataFile;
%% Controls for SubjectID depending on the Mode selected
switch OperationMode
case 'Acquisition'
% Empty subject ID
if isempty(SubjectID)
error('FunctionalLocalizer:MissingSubjectID','\n For acquisition, SubjectID is required \n')
end
% Acquisition => save data
if ~get(handles.radiobutton_SaveData,'Value')
warning('FunctionalLocalizer:DataShouldBeSaved','\n\n\n In acquisition mode, data should be saved \n\n\n')
end
end
%% Parallel port ?
switch get( handles.checkbox_ParPort , 'Value' )
case 1
ParPort = 'On';
case 0
ParPort = 'Off';
end
handles.ParPort = ParPort;
DataStruct.ParPort = ParPort;
%% Check if Eyelink toolbox is available
switch get(get(handles.uipanel_EyelinkMode,'SelectedObject'),'Tag')
case 'radiobutton_EyelinkOff'
EyelinkMode = 'Off';
case 'radiobutton_EyelinkOn'
EyelinkMode = 'On';
% 'Eyelink.m' exists ?
status = which('Eyelink.m');
if isempty(status)
error('FunctionalLocalizer:EyelinkToolbox','no ''Eyelink.m'' detected in the path')
end
% Save mode ?
if strcmp(DataStruct.SaveMode,'NoSave')
error('FunctionalLocalizer:SaveModeForEyelink',' \n ---> Save mode should be turned on when using Eyelink <--- \n ')
end
% Eyelink connected ?
Eyelink.IsConnected
% File name for the eyelink : 8 char maximum
switch Task
case 'EyelinkCalibration'
task = 'E';
case 'Calibration'
task = 'C';
case 'Instructions'
task = 'I';
case 'Session'
task = 'S';
otherwise
error('FunctionalLocalizer:Task','Task ?')
end
EyelinkFile = [ SubjectID task sprintf('%.2d',str2double(RunNumber)) ];
DataStruct.EyelinkFile = EyelinkFile;
otherwise
warning('FunctionalLocalizer:EyelinkMode','Error in Eyelink mode')
end
DataStruct.EyelinkMode = EyelinkMode;
%% Security : NEVER overwrite a file
% If erasing a file is needed, we need to do it manually
if strcmp(SaveMode,'SaveData') && strcmp(OperationMode,'Acquisition')
if exist([DataFile '.mat'], 'file')
error('MATLAB:FileAlreadyExists',' \n ---> \n The file %s.mat already exists . <--- \n \n',DataFile);
end
end
%% Get stimulation parameters
DataStruct.Parameters = GetParameters( DataStruct );
% Screen mode selection
AvalableDisplays = get(handles.listbox_Screens,'String');
SelectedDisplay = get(handles.listbox_Screens,'Value');
DataStruct.Parameters.Video.ScreenMode = str2double( AvalableDisplays(SelectedDisplay) );
%% Windowed screen ?
switch get(handles.checkbox_WindowedScreen,'Value')
case 1
WindowedMode = 'On';
case 0
WindowedMode = 'Off';
otherwise
warning('FunctionalLocalizer:WindowedScreen','Error in WindowedScreen')
end
DataStruct.WindowedMode = WindowedMode;
%% Open PTB window & sound
DataStruct.PTB = StartPTB( DataStruct );
%% Task run
switch Task
case 'EyelinkCalibration'
Eyelink.Calibration( DataStruct.PTB.wPtr );
TaskData.ER.Data = {};
TaskData.IsEyelinkRreadyToRecord = 1;
case 'Calibration'
TaskData = Calibration.Task( DataStruct );
case 'Instructions'
TaskData = Instructions.Task( DataStruct );
case 'Session'
TaskData = Session.Task( DataStruct );
otherwise
error('FunctionalLocalizer:Task','Task ?')
end
DataStruct.TaskData = TaskData;
%% Save files on the fly : just a security in case of crash of the end the script
save([fileparts(pwd) filesep 'data' filesep 'LastDataStruct'],'DataStruct');
%% Close PTB
% Just to be sure that if there is a problem with PTB, we do not loose all
% the data drue to a crash.
try
Screen('CloseAll'); % Close PTB window
Priority( DataStruct.PTB.oldLevel );
catch err
end
%% SPM data organization
[ names , onsets , durations ] = SPMnod( DataStruct ); %#ok<*NASGU,*ASGLU>
%% Saving data strucure
if strcmp(SaveMode,'SaveData') && strcmp(OperationMode,'Acquisition')
if ~exist(DataPath, 'dir')
mkdir(DataPath);
end
save(DataFile, 'DataStruct', 'names', 'onsets', 'durations');
save([DataFile '_SPM'], 'names', 'onsets', 'durations');
% BrainVoyager data organization
% spm2bv( names , onsets , durations , DataStruct.DataFile )
end
%% Send DataStruct and SPM nod to workspace
assignin('base', 'DataStruct', DataStruct);
assignin('base', 'names', names);
assignin('base', 'onsets', onsets);
assignin('base', 'durations', durations);
%% End recording of Eyelink
% Eyelink mode 'On' ?
if strcmp(DataStruct.EyelinkMode,'On')
% Stop recording and retrieve the file
Eyelink.StopRecording( DataStruct.EyelinkFile , DataStruct.DataPath )
if ~strcmp(DataStruct.Task,'EyelinkCalibration')
% Rename the file
movefile([DataStruct.DataPath filesep EyelinkFile '.edf'], [DataStruct.DataFile '.edf'])
end
end
%% Ready for another run
set(handles.text_LastFileNameAnnouncer,'Visible','on')
set(handles.text_LastFileName,'Visible','on')
set(handles.text_LastFileName,'String',DataFile(length(DataPath)+1:end))
WaitSecs(0.100);
fprintf('\n')
fprintf('------------------------- \n')
fprintf('Ready for another session \n')
fprintf('------------------------- \n')
end % function