Skip to content

Commit ecc0245

Browse files
committed
refactor saveEvents
1 parent e7db055 commit ecc0245

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

saveEventsFile.m

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,7 @@
7777
checklLogFile('checkID', logFile);
7878
checklLogFile('type&size', logFile);
7979

80-
% appends to the logfile all the data stored in the structure
81-
% first with the standard BIDS data and then any extra things
82-
for iEvent = 1:size(logFile, 1)
83-
84-
logFile = checklLogFile('fields', logFile, iEvent);
85-
86-
onset = logFile(iEvent).onset;
87-
duration = logFile(iEvent).duration;
88-
trial_type = logFile(iEvent).trial_type;
89-
90-
if isnan(onset) || ischar(onset) || any(isempty({onset trial_type})) || ...
91-
strcmp(trial_type, 'n/a')
92-
93-
warning('\nSkipping saving this event.\n onset: %f \n trial_type: %s\n', ...
94-
onset, ...
95-
trial_type);
96-
97-
else
98-
99-
printData(logFile(1).fileID, onset);
100-
printData(logFile(1).fileID, duration);
101-
printData(logFile(1).fileID, trial_type);
102-
103-
printExtraColumns(logFile, iEvent);
104-
105-
fprintf(logFile(1).fileID, '\n');
106-
107-
end
108-
end
80+
logFile = saveToLogFile(logFile);
10981

11082
case 'close'
11183

@@ -153,14 +125,10 @@
153125

154126
case 'fields'
155127

156-
if ~isfield(logFile, 'onset') || isempty(logFile(iEvent).onset)
157-
logFile(iEvent).onset = nan;
158-
end
159-
if ~isfield(logFile, 'trial_type') || isempty(logFile(iEvent).trial_type)
160-
logFile(iEvent).trial_type = nan;
161-
end
162-
if ~isfield(logFile, 'duration') || isempty(logFile(iEvent).duration)
163-
logFile(iEvent).duration = nan;
128+
for iFields = {'onset', 'trial_type', 'duration'}
129+
if ~isfield(logFile, iFields) || isempty(logFile(iEvent).(iFields{1}))
130+
logFile(iEvent).(iFields{1}) = nan;
131+
end
164132
end
165133

166134
logFile = checkExtracolumns(logFile, iEvent);
@@ -232,7 +200,9 @@ function printHeaderExtraColumns(logFile)
232200
data = logFile(iEvent).(namesExtraColumns{iExtraColumn});
233201
end
234202

235-
data = checkInput(data, nbCol);
203+
data = checkInput(data);
204+
205+
data = nanPadding(data, nbCol);
236206

237207
logFile(iEvent).(namesExtraColumns{iExtraColumn}) = data;
238208

@@ -248,15 +218,11 @@ function printHeaderExtraColumns(logFile)
248218

249219
end
250220

251-
function data = checkInput(data, expectedLength)
221+
function data = checkInput(data)
252222
% check the data to write
253223
% default will be 'n/a' for chars and NaN for numeric data
254224
% for numeric data that don't have the expected length, it will be padded with NaNs
255225

256-
if nargin < 2
257-
expectedLength = [];
258-
end
259-
260226
if islogical(data) && data
261227
data = 'true';
262228
elseif islogical(data) && ~data
@@ -271,12 +237,54 @@ function printHeaderExtraColumns(logFile)
271237
data = nan;
272238
end
273239

240+
end
241+
242+
function data = nanPadding(data, expectedLength)
243+
244+
if nargin < 2
245+
expectedLength = [];
246+
end
247+
274248
if ~isempty(expectedLength) && isnumeric(data) && max(size(data)) < expectedLength
275249
padding = expectedLength - max(size(data));
276250
data(end + 1:end + padding) = nan(1, padding);
277251
elseif ~isempty(expectedLength) && isnumeric(data) && max(size(data)) > expectedLength
278-
data = data(1:expectedLength);
279252
warning('A field for this event is longer than expected. Truncating the extra values.');
253+
data = data(1:expectedLength);
254+
end
255+
256+
end
257+
258+
function logFile = saveToLogFile(logFile)
259+
260+
% appends to the logfile all the data stored in the structure
261+
% first with the standard BIDS data and then any extra things
262+
for iEvent = 1:size(logFile, 1)
263+
264+
logFile = checklLogFile('fields', logFile, iEvent);
265+
266+
onset = logFile(iEvent).onset;
267+
duration = logFile(iEvent).duration;
268+
trial_type = logFile(iEvent).trial_type;
269+
270+
if isnan(onset) || ischar(onset) || any(isempty({onset trial_type})) || ...
271+
strcmp(trial_type, 'n/a')
272+
273+
warning('\nSkipping saving this event.\n onset: %f \n trial_type: %s\n', ...
274+
onset, ...
275+
trial_type);
276+
277+
else
278+
279+
printData(logFile(1).fileID, onset);
280+
printData(logFile(1).fileID, duration);
281+
printData(logFile(1).fileID, trial_type);
282+
283+
printExtraColumns(logFile, iEvent);
284+
285+
fprintf(logFile(1).fileID, '\n');
286+
287+
end
280288
end
281289

282290
end

0 commit comments

Comments
 (0)