forked from xiangruili/dicm2nii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_tsv.m
60 lines (56 loc) · 2.01 KB
/
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
function write_tsv(id,tsvfile,varargin)
% write_tsv write a tsv (tabulated-separated) file
% if tsvfile already exists, replace value at the line defined
% by an id (first column) or add a new line
%
% write_tsv(id,tsvfile,varargin)
% Example:
% write_tsv('Pierre','stats.tsv','age',50)
% write_tsv('Jean', 'stats.tsv','age',20)
% write_tsv('Jean', 'stats.tsv','height',180)
if iscell(tsvfile), tsvfile = tsvfile{1}; end
if exist(tsvfile,'file') % read already existing tsvfile
% Number of columns
fid = fopen(tsvfile);
tline = fgetl(fid);
fclose(fid);
Nvar = sum(~cellfun(@isempty,strsplit(tline,'\t')));
% read tsv file
T = readtable(tsvfile,'FileType','text','Delimiter','\t','Format',repmat('%s',[1,Nvar]));
end
varargin(1:2:end) = cellfun(@genvarname,varargin(1:2:end),'uni',0);
varargin(cellfun(@isempty,varargin)) = {'N/A'};
if exist(tsvfile,'file') && ~isempty(T) % append to already existing tsvfile
warning('OFF', 'MATLAB:table:RowsAddedExistingVars');
ind = find(strcmp(table2cell(T(:,1)),id),1);
if isempty(ind)
ind = size(T,1)+1;
T.(T.Properties.VariableNames{1}){end+1,1} = char(id);
end
for ii=1:2:length(varargin)
if ismember(varargin{ii},T.Properties.VariableNames)
else
if isnumeric(varargin{ii+1})
T.(varargin{ii}) = nan(size(T,1),1);
else
T.(varargin{ii}) = cell(size(T,1),1);
end
end
if iscell(T.(varargin{ii}))
T.(varargin{ii}){ind} = varargin{ii+1};
else
T.(varargin{ii})(ind) = varargin{ii+1};
end
end
else % write new tsvfile
T=table;
T(end+1,:) = {char(id), varargin{2:2:end}};
if isempty(inputname(1))
idName = 'id';
else
idName = inputname(1);
end
T.Properties.VariableNames = {idName varargin{1:2:end}};
warning('ON', 'MATLAB:table:RowsAddedExistingVars');
end
writetable(T,tsvfile,'Delimiter','\t','FileType','text')