-
Notifications
You must be signed in to change notification settings - Fork 16
/
bids_writebehfile.m
62 lines (54 loc) · 1.57 KB
/
bids_writebehfile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
% BIDS_WRITEBEHFILE - write behavioral file
%
% Usage:
% bids_writebehfile(beh, fileOut)
%
%
% Inputs:
% beh - [struct] structure containing behavioral data (event
% structure for example)
% behinfo - [struct] column names and description (optional)
% fileOut - [string] filepath of the desired output location with file basename
% e.g. ~/BIDS_EXPORT/sub-01/ses-01/eeg/sub-01_ses-01_task-GoNogo
%
% Authors: Arnaud Delorme, 2022
function bids_writebehfile(beh, behinfo, fileOut)
if isempty(beh)
return;
end
if ~contains(fileOut, '_beh.tsv')
fileOut = [ fileOut '_beh.tsv'];
end
folder = fileparts(fileOut);
if ~exist(folder)
mkdir(folder);
end
fid = fopen( fileOut, 'w');
if fid == -1
error('Cannot open behavioral file');
end
fields = fieldnames(beh);
for iField = 1:length(fields)
fprintf(fid, '%s', fields{iField} );
if iField < length(fields), fprintf(fid, '\t'); end
end
fprintf(fid, '\n');
for iRow = 1:length(beh)
for iField = 1:length(fields)
if isempty(beh(iRow).(fields{iField})) || any(isnan(beh(iRow).(fields{iField})))
fprintf(fid, 'n/a' );
else
if ischar(beh(iRow).(fields{iField}))
fprintf(fid, '%s', beh(iRow).(fields{iField}) );
else
fprintf(fid, '%1.4f', beh(iRow).(fields{iField}) );
end
end
if iField < length(fields), fprintf(fid, '\t'); end
end
fprintf(fid, '\n');
end
fclose(fid);
if ~isempty(behinfo)
jsonwrite([fileOut(1:end-4) '.json'], behinfo, struct('indent',' '));
end