8686 case ' init'
8787
8888 % flag to indicate that this will be an _events file
89- logFile(1 ).isStim = 0 ;
89+ logFile(1 ).isStim = false ;
9090
91- logFile(1 ).filename = cfg .fileName .events ;
92-
93- logFile = initializeFile(cfg , logFile );
91+ if isfield(cfg ,' fileName' ) && ...
92+ isfield(cfg .fileName ,' events' ) && ...
93+ ~isempty(cfg .fileName .events )
94+ logFile(1 ).filename = cfg .fileName .events ;
95+ else
96+ logFile(1 ).filename = ' ' ;
97+ end
98+ logFile = initializeFile(logFile );
9499
95100 case ' init_stim'
96101
97102 % flag to indicate that this will be an _stim file
98- logFile(1 ).isStim = 1 ;
103+ logFile(1 ).isStim = true ;
99104
100- logFile(1 ).filename = cfg .fileName .stim ;
101-
102- logFile = initializeStimFile(cfg , logFile );
105+ if isfield(cfg ,' fileName' ) && ...
106+ isfield(cfg .fileName ,' stim' ) && ...
107+ ~isempty(cfg .fileName .stim )
108+ logFile(1 ).filename = cfg .fileName .stim ;
109+ else
110+ logFile(1 ).filename = ' ' ;
111+ end
112+ logFile = initializeStimFile(logFile );
103113
104114 case ' open'
105115
167177
168178end
169179
170- function logFile = initializeFile(cfg , logFile )
180+ function logFile = initializeFile(logFile )
171181% This function creates the bids field structure for json files for the
172182% three basic bids event columns, and for all requested extra columns.
173183%
178188 logFile(1 ).columns = struct( ...
179189 ' onset' , struct( ...
180190 ' Description' , ' time elapsed since experiment start' , ...
181- ' Unit ' , ' s' ), ...
191+ ' Units ' , ' s' ), ...
182192 ' trial_type' , struct( ...
183193 ' Description' , ' types of trial' , ...
184194 ' Levels' , ' ' ), ...
185195 ' duration' , struct( ...
186196 ' Description' , ' duration of the event or the block' , ...
187- ' Unit ' , ' s' ) ...
197+ ' Units ' , ' s' ) ...
188198 );
189199
190200
191201 logFile = initializeExtraColumns(logFile );
192202
193203end
194204
195- function logFile = initializeStimFile(cfg , logFile )
205+ function logFile = initializeStimFile(logFile )
196206
197207 logFile = initializeExtraColumns(logFile );
198208
211221 logFile .filename ), ...
212222 ' w' );
213223
214- if logFile(1 ).isStim == 0
224+ if ~ logFile(1 ).isStim
215225 % print the basic BIDS columns
216226 fprintf(logFile(1 ).fileID, ' %s\t%s\t%s ' , ' onset' , ' duration' , ' trial_type' );
217227 fprintf(1 , ' %s\t%s\t%s ' , ' onset' , ' duration' , ' trial_type' );
222232 fprintf(logFile(1 ).fileID, ' \n ' );
223233 fprintf(1 , ' \n ' );
224234
225- elseif logFile(1 ).isStim == 1
235+ elseif logFile(1 ).isStim
226236 % don't print column headers for _stim.tsv
227237
228238 end
@@ -334,6 +344,7 @@ function printHeaderExtraColumns(logFile)
334344
335345function logFile = saveToLogFile(logFile , cfg )
336346
347+
337348 % appends to the logfile all the data stored in the structure
338349 % first with the standard BIDS data and then any extra things
339350 for iEvent = 1 : size(logFile , 1 )
@@ -345,7 +356,7 @@ function printHeaderExtraColumns(logFile)
345356
346357 % if this is _events file, we skip events with onset or duration
347358 % that are empty, nan or char.
348- if logFile(1 ).isStim== 0
359+ if ~ logFile(1 ).isStim
349360
350361 onset = logFile(iEvent ).onset;
351362 duration = logFile(iEvent ).duration;
@@ -357,40 +368,48 @@ function printHeaderExtraColumns(logFile)
357368
358369 skipEvent = true ;
359370
360- warningMessage = sprintf([' saveEventsFile:emptyEvent' , ...
361- ' \n Skipping saving this event.\n onset: %s \n duration: %s\n ' ], ...
362- onset , ...
363- duration );
371+ warningMessageID = ' saveEventsFile:emptyEvent' ;
372+ warningMessage = sprintf([' Skipping saving this event. \n ' ...
373+ ' onset: %s \n duration: %s \n ' ], ...
374+ onset , ...
375+ duration );
364376 end
365377
366378 % if this is _stim file, we skip missing events (i.e. events where
367379 % all extra columns have NO values)
368- elseif logFile(1 ).isStim== 1
380+ elseif logFile(1 ).isStim
369381
370382 namesExtraColumns = returnNamesExtraColumns(logFile );
371- isValid = ones(1 ,numel(namesExtraColumns ))
383+ isValid = ones(1 ,numel(namesExtraColumns ));
372384 for iExtraColumn = 1 : numel(namesExtraColumns )
373385 data = logFile(iEvent ).(namesExtraColumns{iExtraColumn });
374386 if isempty(data ) || isnan(data ) || ( ischar(data ) && strcmp(data ,' n/a' ) )
375387 isValid(iExtraColumn ) = 0 ;
376388 end
377389 end
378- if ~any( isValid )
390+ if all(~ isValid )
379391 skipEvent = true ;
380392
381- warningMessage = sprintf([' saveEventsFile:emptyEvent' , ...
382- ' \n Skipping saving this event.\n No values defined. \n ' ]);
393+ warningMessageID = ' saveEventsFile:emptyEvent' ;
394+ warningMessage = sprintf([' Skipping saving this event. \n ' , ...
395+ ' No values defined. \n ' ]);
396+ elseif any(~isValid )
397+ skipEvent = false ;
398+
399+ warningMessageID = ' saveEventsFile:missingData' ;
400+ warningMessage = sprintf(' Missing some %s data for this event. \n ' , ...
401+ namesExtraColumns{find(isValid )});
383402 end
384403 end
385404
386405 % now save the event to log file (if not skipping)
387406 if skipEvent
388407
389- warning(warningMessage );
408+ warning(warningMessageID , warningMessage );
390409
391410 else
392411
393- if logFile(1 ).isStim== 0
412+ if ~ logFile(1 ).isStim
394413
395414 printData(logFile(1 ).fileID, onset , cfg );
396415 printData(logFile(1 ).fileID, duration , cfg );
@@ -455,7 +474,7 @@ function printData(output, data, cfg)
455474 logFile(2 : end ) = [];
456475
457476 namesColumns = {' onset' , ' duration' , ' trial_type' };
458- if logFile(1 ).isStim == 1
477+ if logFile(1 ).isStim
459478 namesColumns = {};
460479 end
461480 namesExtraColumns = returnNamesExtraColumns(logFile );
0 commit comments