33%
44% INPUTS
55%
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+ %
624% action:
725% - 'open': will create the file ID and return it in logFile.eventLogFile using the information in
826% the expParameters structure. This file ID is then reused when calling that function to save data
2846end
2947
3048switch action
31-
49+
3250 case ' open'
33-
51+
3452 logFile = struct();
35-
53+
3654 % Initialize txt logfiles and empty fields for the standard BIDS
3755 % event file
3856 logFile.eventLogFile = fopen(...
39- fullfile(expParameters .outputDir , expParameters .modality , expParameters .fileName .events ), ...
57+ fullfile(...
58+ expParameters .outputDir , ...
59+ expParameters .modality , ...
60+ expParameters .fileName .events ), ...
4061 ' 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+
5680 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+
5886 % appends to the logfile all the data stored in the structure
5987 % first with the standard BIDS data and then any extra things
6088 for iEvent = 1 : size(logFile ,1 )
61-
89+
6290 fprintf(logFile(1 ).eventLogFile,' %f\t%s\t%f\t ' ,...
6391 logFile(iEvent ).onset, ...
6492 logFile(iEvent ).trial_type, ...
6593 logFile(iEvent ).duration);
66-
94+
6795 for iExtraColumn = 1 : numel(varargin )
68-
96+
6997 % if the field we are looking for does not exist or is empty in the
7098 % action logFile structure we will write a NaN otherwise we
7199 % write its content
72-
100+
73101 if ~isfield(logFile , varargin{iExtraColumn })
74102 data = [];
75103 else
76104 data = getfield(logFile(iEvent ), varargin{iExtraColumn });
77105 end
78-
106+
79107 if isempty(data )
80108 data = NaN ;
81109 end
82-
110+
83111 if ischar(data )
84112 fprintf(logFile(1 ).eventLogFile, ' %s\t ' , data );
85113 else
86114 fprintf(logFile(1 ).eventLogFile, ' %f\t ' , data );
87115 end
88-
116+
89117 end
90-
118+
91119 fprintf(logFile(1 ).eventLogFile, ' \n ' );
92120 end
93-
121+
94122 case ' close'
95-
123+
96124 % close txt log file
97125 fclose(logFile(1 ).eventLogFile);
98-
126+
99127 if expParameters .verbose
100128 fprintf(1 ,' \n Data were saved in this file:\n\n%s\n\n ' , ...
101129 fullfile(...
102130 expParameters .outputDir , ...
103131 expParameters .modality , ...
104132 expParameters .fileName .events ));
105-
133+
106134 end
135+
136+ end
107137
108138end
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