3
3
%
4
4
% INPUTS
5
5
%
6
+ % logFile:
7
+ % When you want to save your data logFile contains the data you want to save.
8
+ % The logFile variable that contains the n events you want to % save must be a nx1 structure.
9
+ % Each field will be saved in a separate column.
10
+ %
11
+ % example:
12
+ % logFile(1,1).onset = 2;
13
+ % logFile(1,1).trial_type = 'motion_up';
14
+ % logFile(1,1).duration = 1;
15
+ % logFile(1,1).speed = 2;
16
+ % logFile(1,1).is_fixation = true;
17
+ %
18
+ % logFile(2,1).onset = 3;
19
+ % logFile(2,1).trial_type = 'static';
20
+ % logFile(2,1).duration = 4;
21
+ % logFile(2,1).is_fixation = 3;
22
+ %
23
+ %
6
24
% action:
7
25
% - 'open': will create the file ID and return it in logFile.eventLogFile using the information in
8
26
% the expParameters structure. This file ID is then reused when calling that function to save data
28
46
end
29
47
30
48
switch action
31
-
49
+
32
50
case ' open'
33
-
51
+
34
52
logFile = struct();
35
-
53
+
36
54
% Initialize txt logfiles and empty fields for the standard BIDS
37
55
% event file
38
56
logFile.eventLogFile = fopen(...
39
- fullfile(expParameters .outputDir , expParameters .modality , expParameters .fileName .events ), ...
57
+ fullfile(...
58
+ expParameters .outputDir , ...
59
+ expParameters .modality , ...
60
+ expParameters .fileName .events ), ...
40
61
' w' );
41
-
42
- % print the basic BIDS columns
43
- fprintf(logFile .eventLogFile , ' %s\t%s\t%s\t ' , ' onset' , ' trial_type' , ' duration' );
44
-
45
- % print any extra column specified by the user
46
- % also prepare an empty field in the structure to collect data
47
- % for those
48
- for iExtraColumn = 1 : numel(varargin )
49
- fprintf(logFile .eventLogFile ,' %s\t ' , lower(varargin{iExtraColumn }));
50
- end
51
-
52
- % next line so we start printing at the right place
53
- fprintf(logFile .eventLogFile , ' \n ' );
54
-
55
-
62
+
63
+ initializeHeader(logFile , varargin );
64
+
65
+
66
+ case ' open_stim'
67
+ logFile = struct();
68
+
69
+ % Initialize txt logfiles and empty fields for the standard BIDS
70
+ % event file
71
+ logFile.eventLogFile = fopen(...
72
+ fullfile(...
73
+ expParameters .outputDir , ...
74
+ expParameters .modality , ...
75
+ expParameters .fileName .stim ), ...
76
+ ' w' );
77
+
78
+ initializeHeader(logFile , varargin );
79
+
56
80
case ' save'
57
-
81
+
82
+ if ~isstruct(logFile ) || size(logFile , 2 )>1
83
+ error(' The logFile variable that contains the n events you want to save must be a nx1 structure.' )
84
+ end
85
+
58
86
% appends to the logfile all the data stored in the structure
59
87
% first with the standard BIDS data and then any extra things
60
88
for iEvent = 1 : size(logFile ,1 )
61
-
89
+
62
90
fprintf(logFile(1 ).eventLogFile,' %f\t%s\t%f\t ' ,...
63
91
logFile(iEvent ).onset, ...
64
92
logFile(iEvent ).trial_type, ...
65
93
logFile(iEvent ).duration);
66
-
94
+
67
95
for iExtraColumn = 1 : numel(varargin )
68
-
96
+
69
97
% if the field we are looking for does not exist or is empty in the
70
98
% action logFile structure we will write a NaN otherwise we
71
99
% write its content
72
-
100
+
73
101
if ~isfield(logFile , varargin{iExtraColumn })
74
102
data = [];
75
103
else
76
104
data = getfield(logFile(iEvent ), varargin{iExtraColumn });
77
105
end
78
-
106
+
79
107
if isempty(data )
80
108
data = NaN ;
81
109
end
82
-
110
+
83
111
if ischar(data )
84
112
fprintf(logFile(1 ).eventLogFile, ' %s\t ' , data );
85
113
else
86
114
fprintf(logFile(1 ).eventLogFile, ' %f\t ' , data );
87
115
end
88
-
116
+
89
117
end
90
-
118
+
91
119
fprintf(logFile(1 ).eventLogFile, ' \n ' );
92
120
end
93
-
121
+
94
122
case ' close'
95
-
123
+
96
124
% close txt log file
97
125
fclose(logFile(1 ).eventLogFile);
98
-
126
+
99
127
if expParameters .verbose
100
128
fprintf(1 ,' \n Data were saved in this file:\n\n%s\n\n ' , ...
101
129
fullfile(...
102
130
expParameters .outputDir , ...
103
131
expParameters .modality , ...
104
132
expParameters .fileName .events ));
105
-
133
+
106
134
end
135
+
136
+ end
107
137
108
138
end
139
+
140
+ function initializeHeader(logFile , varargin )
141
+
142
+ % print the basic BIDS columns
143
+ fprintf(logFile .eventLogFile , ' %s\t%s\t%s\t ' , ' onset' , ' trial_type' , ' duration' );
144
+
145
+ % print any extra column specified by the user
146
+ % also prepare an empty field in the structure to collect data
147
+ % for those
148
+ for iExtraColumn = 1 : numel(varargin{1 })
149
+ fprintf(logFile .eventLogFile ,' %s\t ' , lower(varargin{1 }{iExtraColumn }));
150
+ end
151
+
152
+ % next line so we start printing at the right place
153
+ fprintf(logFile .eventLogFile , ' \n ' );
154
+
155
+ end
0 commit comments