-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbids_participants_tsv.m
More file actions
59 lines (48 loc) · 2 KB
/
bids_participants_tsv.m
File metadata and controls
59 lines (48 loc) · 2 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
% bids_participants_tsv - This function takes adata table input and
% creates the participants.tsv file or appends the
% table to an existing participants.tsv file.
%
% Usage:
% >> bids_participants_tsv( dt, tsv_file)
%
% Required inputs:
% dt - data table object containing information about a
% participant.
% tsv_file - tab-separated (tsv) file to write dt to. If this file
% exists, it is loaded and then the DT input is appended to
% the table.
%
% Create by: Joshua D. Koen, University of Notre Dame
% Created on 2019/06/17
function bids_participants_tsv( dt, tsv_file )
% Error check
if ~istable(dt), error('first input must be a data table object.'); end
if height(dt) ~= 1, error('dt must only have one row of data.'); end
if ~strcmpi(tsv_file(end-3:end), '.tsv'), error('tsv_file must have tab-separated extension (.tsv)'); end
% If file name exists, load it
if isfile(tsv_file)
% Load file if necessary
opts = detectImportOptions(tsv_file, 'FileType', 'text');
par_dt = readtable(tsv_file, opts);
% Error check
if ~isequal(par_dt.Properties.VariableNames, dt.Properties.VariableNames)
error('fields for input data table and existing data table in tsv_file do not match.')
end
% Convert par_dt to cell array, then back (after appending)
% This is a 'trick' to avoid some columns being interpreted as character
% matrices.
columns = par_dt.Properties.VariableNames;
par_cell = table2cell(par_dt);
input_cell = table2cell(dt);
par_col = strcmpi(columns,'participant_id');
if ~ismember(par_cell(:,par_col), input_cell(:,par_col))
par_cell = vertcat(par_cell,input_cell)
end
par_dt = cell2table(par_cell, 'VariableNames', columns);
else % Otherwise, load dt as cell
par_dt = dt;
par_cell = table2cell(par_dt);
end
% Write to file
writetable(par_dt,tsv_file,'FileType','text','Delimiter','\t');
end % of function