-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbids_events_to_tsv.m
More file actions
77 lines (66 loc) · 2.34 KB
/
bids_events_to_tsv.m
File metadata and controls
77 lines (66 loc) · 2.34 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
% bids_events_to_tsv - This function takes an EEG data structure as input,
% and writes inofrmation in EEG.events to a
% *events.tsv sidecar file.
%
% Usage:
% >> bids_events_to_tsv( EEG )
% >> bids_events_to_tsv( EEG, tsv_file )
% >> bids_events_to_tsv( EEG, tsv_file, vars_to_write )
%
% Required inputs:
% EEG - EEG data structure with EEG.event field
%
%
% Optional inputs:
% tsv_file - file name of data file to write *channels.tsv. Defaults
% to reading EEG.filepath and EEG.filename.
% vars_to_write - cell array of strings for fields in EEG.event to write
% to the sidecar. These are ADDITIONAL fields. Defaults
% are onset, duration, sample, and value (following BIDS
% specification).
%
% Create by: Joshua D. Koen, University of Notre Dame
% Created on 2019/06/17
function bids_events_to_tsv( EEG, tsv_file, vars_to_write )
% Just convert EEG.event to cell table to start off with
% This is useful for extracting data later on
in_dt = struct2table( EEG.event );
% Extract onsets, samples, and durations
onset = (in_dt.latency / EEG.srate) - (1 / EEG.srate);
sample = in_dt.latency;
duration = in_dt.duration;
value = in_dt.type;
% create output dt (out_dt)
out_dt = table(onset,sample,duration,value);
% Add additional columns if need be
if isvarname('vars_to_write') && ~isempty(vars_to_write)
% Error check vars_to_write
if ~all( ismember(vars_to_write, in_dt.Properties.VariableNames) )
error('All VARS_TO_WRITE input values must exist as a field in EEG.event.');
end
% Update out_dt
for vari = 1:length(vars_to_write)
this_field = vars_to_write{vari};
out_dt.( this_field ) = in_dt.( this_field );
end
end
% If file name is supplied, use it to make _events.tsv sidecar
if isvarname('filename') && ~isempty(tsv_file)
[path, file] = fileparts(tsv_file);
else
if isempty(EEG.filepath)
path = pwd;
else
path = EEG.filepath;
end
if isempty(EEG.filename)
file = '';
else
[~,file] = fileparts(EEG.filename);
end
end
file = strrep( file, '_eeg', '_events.tsv' );
% Write to file
events_tsv_name = fullfile( path, file );
writetable(out_dt,events_tsv_name,'FileType','text','Delimiter','\t');
end % of function