7272 %
7373 %
7474
75- if nargin < 1
76- error(' Missing action input' );
77- end
78-
7975 if nargin < 2
80- cfg = struct();
76+ error([' Missing arguments. Please specify <action input> ' ,...
77+ ' and <cfg file> as the first two arguments' ]);
8178 end
8279
8380 if nargin < 3 || isempty(logFile )
8986 case ' init'
9087
9188 % flag to indicate that this will be an _events file
92- logFile.isStim = 0 ;
89+ logFile( 1 ) .isStim = 0 ;
9390
9491 logFile(1 ).filename = cfg .fileName .events ;
9592
9895 case ' init_stim'
9996
10097 % flag to indicate that this will be an _stim file
101- logFile.isStim = 1 ;
98+ logFile( 1 ) .isStim = 1 ;
10299
103100 logFile(1 ).filename = cfg .fileName .stim ;
104101
214211 logFile .filename ), ...
215212 ' w' );
216213
217- if logFile(1 , 1 ).isStim == 0
214+ if logFile(1 ).isStim == 0
218215 % print the basic BIDS columns
219216 fprintf(logFile(1 ).fileID, ' %s\t%s\t%s ' , ' onset' , ' duration' , ' trial_type' );
220217 fprintf(1 , ' %s\t%s\t%s ' , ' onset' , ' duration' , ' trial_type' );
225222 fprintf(logFile(1 ).fileID, ' \n ' );
226223 fprintf(1 , ' \n ' );
227224
228- elseif logFile(1 , 1 ).isStim == 1
225+ elseif logFile(1 ).isStim == 1
229226 % don't print column headers for _stim.tsv
230227
231228 end
@@ -343,9 +340,12 @@ function printHeaderExtraColumns(logFile)
343340
344341 logFile = checklLogFile(' fields' , logFile , iEvent , cfg );
345342
343+ % check if this event should be skipped
344+ skipEvent = false ;
345+
346346 % if this is _events file, we skip events with onset or duration
347347 % that are empty, nan or char.
348- if logFile(1 , 1 ).isStim== 0
348+ if logFile(1 ).isStim== 0
349349
350350 onset = logFile(iEvent ).onset;
351351 duration = logFile(iEvent ).duration;
@@ -355,41 +355,54 @@ function printHeaderExtraColumns(logFile)
355355 any(cellfun(@ischar , {onset duration })) || ...
356356 any(isempty({onset duration }))
357357
358- warning(' saveEventsFile:emptyEvent' , ...
359- ' \n Skipping saving this event.\n onset: %s \n duration: %s\n ' , ...
358+ skipEvent = true ;
359+
360+ warningMessage = sprintf([' saveEventsFile:emptyEvent' , ...
361+ ' \n Skipping saving this event.\n onset: %s \n duration: %s\n ' ], ...
360362 onset , ...
361363 duration );
362- else
363- printData(logFile(1 ).fileID, onset , cfg );
364- printData(logFile(1 ).fileID, duration , cfg );
365- printData(logFile(1 ).fileID, trial_type , cfg );
366-
367- printExtraColumns(logFile , iEvent , cfg );
368-
369- fprintf(logFile(1 ).fileID, ' \n ' );
370- fprintf(1 , ' \n ' );
371-
372364 end
373365
374366 % if this is _stim file, we skip missing events (i.e. events where
375367 % all extra columns have NO values)
376- elseif logFile(1 , 1 ).isStim== 1
368+ elseif logFile(1 ).isStim== 1
377369
378- skipEvent = false ;
379370 namesExtraColumns = returnNamesExtraColumns(logFile );
371+ isValid = ones(1 ,numel(namesExtraColumns ))
380372 for iExtraColumn = 1 : numel(namesExtraColumns )
381373 data = logFile(iEvent ).(namesExtraColumns{iExtraColumn });
382374 if isempty(data ) || isnan(data ) || ( ischar(data ) && strcmp(data ,' n/a' ) )
383- skipEvent = true ;
375+ isValid( iExtraColumn ) = 0 ;
384376 end
385377 end
386- if ~skipEvent
387- printExtraColumns(logFile , iEvent , cfg );
378+ if ~any(isValid )
379+ skipEvent = true ;
380+
381+ warningMessage = sprintf([' saveEventsFile:emptyEvent' , ...
382+ ' \n Skipping saving this event.\n No values defined. \n ' ]);
383+ end
384+ end
385+
386+ % now save the event to log file (if not skipping)
387+ if skipEvent
388+
389+ warning(warningMessage );
390+
391+ else
392+
393+ if logFile(1 ).isStim== 0
394+
395+ printData(logFile(1 ).fileID, onset , cfg );
396+ printData(logFile(1 ).fileID, duration , cfg );
397+ printData(logFile(1 ).fileID, trial_type , cfg );
388398
389- fprintf(logFile(1 ).fileID, ' \n ' );
390- fprintf(1 , ' \n ' );
391399 end
392400
401+ printExtraColumns(logFile , iEvent , cfg );
402+
403+ fprintf(logFile(1 ).fileID, ' \n ' );
404+ fprintf(1 , ' \n ' );
405+
393406 end
394407
395408 end
@@ -441,9 +454,8 @@ function printData(output, data, cfg)
441454
442455 logFile(2 : end ) = [];
443456
444- if logFile(1 ,1 ).isStim == 0
445- namesColumns = {' onset' , ' duration' , ' trial_type' };
446- elseif logFile(1 ,1 ).isStim == 1
457+ namesColumns = {' onset' , ' duration' , ' trial_type' };
458+ if logFile(1 ).isStim == 1
447459 namesColumns = {};
448460 end
449461 namesExtraColumns = returnNamesExtraColumns(logFile );
0 commit comments