Skip to content

Commit 36300c9

Browse files
committed
add more input checks and error handling to saveEvents
1 parent aed7ec0 commit 36300c9

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

saveEventsFile.m

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
end
4848

4949
if nargin < 3 || isempty(logFile)
50-
logFile = struct('filename', [], 'extraColumns', cell(1));
51-
logFile(1).filename = '';
50+
logFile = checklLogFile('init');
5251
end
5352

5453
switch action
@@ -67,9 +66,8 @@
6766

6867
case 'save'
6968

70-
if ~isstruct(logFile) || size(logFile, 2) > 1
71-
error('The variable containing the n events to save must be a nx1 structure.');
72-
end
69+
checklLogFile('checkID', logFile);
70+
checklLogFile('type&size', logFile);
7371

7472
% appends to the logfile all the data stored in the structure
7573
% first with the standard BIDS data and then any extra things
@@ -103,6 +101,8 @@
103101

104102
case 'close'
105103

104+
checklLogFile('checkID', logFile);
105+
106106
% close txt log file
107107
fclose(logFile(1).fileID);
108108

@@ -113,16 +113,40 @@
113113
logFile.filename));
114114

115115
otherwise
116-
errorStruct.message = 'unknown action for saveEventsFile';
117-
errorStruct.identifier = 'saveEventsFile:unknownActionType';
118-
error(errorStruct);
116+
117+
errorSaveEventsFile('unknownActionType');
119118

120119
end
121120

122121
logFile = resetLogFileVar(logFile);
123122

124123
end
125124

125+
function logFile = checklLogFile(action, logFile)
126+
127+
switch action
128+
129+
case 'init'
130+
131+
logFile = struct('filename', [], 'extraColumns', cell(1));
132+
logFile(1).filename = '';
133+
134+
case 'checkID'
135+
136+
if ~isfield(logFile(1), 'fileID') || isempty(logFile(1).fileID)
137+
errorSaveEventsFile('missingFileID');
138+
end
139+
140+
case 'type&size'
141+
142+
if ~isstruct(logFile) || size(logFile, 2) > 1
143+
errorSaveEventsFile('wrongFileID');
144+
end
145+
146+
end
147+
148+
end
149+
126150
function logFile = initializeFile(expParameters, logFile)
127151

128152
% Initialize txt logfiles and empty fields for the standard BIDS
@@ -285,3 +309,21 @@ function printExtraColumns(logFile, iEvent)
285309
end
286310

287311
end
312+
313+
function errorSaveEventsFile(identifier)
314+
315+
switch identifier
316+
case 'unknownActionType'
317+
errorStruct.message = 'unknown action for saveEventsFile';
318+
319+
case 'missingFileID'
320+
errorStruct.message = 'logFile must contain a valid fileID field';
321+
322+
case 'wrongFileID'
323+
errorStruct.message = 'logFile must be a nx1 structure';
324+
325+
end
326+
327+
errorStruct.identifier = ['saveEventsFile:' unknownActionType];
328+
error(errorStruct);
329+
end

tests/miss_hit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
line_length: 100
2-
regex_function_name: "((test_[a-z]+)|[a-z]+)(([A-Z]|[0-9]){1}[a-z]+)*"
2+
regex_function_name: "((test_[a-z]+)|[a-z]+)(([A-Z]){1}[A-Za-z]+)*"
33
suppress_rule: "copyright_notice"

tests/test_saveEventsFileOpen.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ function test_saveEventsFileOpen()
4444
assert(isequal(C{1}{1}, 'onset'));
4545
assert(isequal(C{2}{1}, 'trial_type'));
4646
assert(isequal(C{3}{1}, 'duration'));
47-
48-
49-
47+
5048
%% check header writing with extra columns
5149
fprintf('\n\n--------------------------------------------------------------------\n\n');
5250

0 commit comments

Comments
 (0)