-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathft_write_tsv.m
76 lines (71 loc) · 2.5 KB
/
ft_write_tsv.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function ft_write_tsv(filename, tsv)
% FT_WRITE_TSV writes a MATLAB table to a tab-separated-values file. Compared to the
% builtin MATLAB function, this implementation deals a bit different with missing
% values, booleans, and NaNs.
%
% Use as
% ft_write_tsv(filename, table)
%
% See also FT_READ_TSV, FT_READ_JSON, FT_WRITE_JSON, READTABLE, WRITETABLE, TBLWRITE
% Copyright (C) 2018-2024, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
ft_info('writing ''%s''\n', filename);
fn = tsv.Properties.VariableNames;
for i=1:numel(fn)
% write [] as 'n/a'
% write nan as 'n/a'
% write boolean as 'True' or 'False'
tsv.(fn{i}) = output_compatible(tsv.(fn{i}));
end
writetable(tsv, filename, 'Delimiter', 'tab', 'FileType', 'text');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION this is shared with DATA2BIDS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function val = output_compatible(val)
if istable(val)
fn = val.Properties.VariableNames;
for i=1:numel(fn)
val.(fn{i}) = output_compatible(val.(fn{i}));
end
elseif iscell(val)
% use recursion to make all elements compatible
val = cellfun(@output_compatible, val, 'UniformOutput', false);
elseif isnumeric(val) && numel(val)>1 && any(isnan(val))
% convert and use recursion to make all elements compatible
val = num2cell(val);
val = cellfun(@output_compatible, val, 'UniformOutput', false);
elseif isdatetime(val)
val = datestr(val, 'yyyy-mm-ddTHH:MM:SS.FFF'); % following RFC3339 and BIDS
else
% write [] as 'n/a'
% write nan as 'n/a'
% write boolean as 'true' or 'talse'
if isempty(val)
val = 'n/a';
elseif isnan(val)
val = 'n/a';
elseif islogical(val)
if val
val = 'true';
else
val = 'false';
end
end
end