Skip to content

Commit 4140f15

Browse files
committed
forcing index 1 in indexed group names in savesnirf, see fNIRS/snirf_homer3#4
1 parent c46eb32 commit 4140f15

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

savesnirf.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ function savesnirf(data, outfile,varargin)
4444
end
4545

4646
if(~isempty(outfile))
47-
if(~isempty(regexp(outfile,'\.[Hh]5$', 'once')) || ~isempty(regexp(outfile,'\.[Ss][Nn][Ii][Rr][Ff]$', 'once')))
47+
if(~isempty(regexp(outfile,'\.[Hh]5$', 'once')))
48+
saveh5(data,outfile,opt);
49+
elseif(~isempty(regexp(outfile,'\.[Ss][Nn][Ii][Rr][Ff]$', 'once')))
50+
data.nirs.data=forceindex(data.nirs.data,'measurementList');
51+
data.nirs=forceindex(data.nirs,'data');
52+
data.nirs=forceindex(data.nirs,'stim');
53+
data.nirs=forceindex(data.nirs,'aux');
4854
saveh5(data,outfile,opt);
4955
elseif(~isempty(regexp(outfile,'\.[Jj][Nn][Ii][Rr][Ss]$', 'once'))|| ~isempty(regexp(outfile,'\.[Jj][Ss][Oo][Nn]$', 'once')))
5056
savejson('SNIRDData',data,'FileName',outfile,opt);
@@ -56,3 +62,16 @@ function savesnirf(data, outfile,varargin)
5662
error('only support .snirf, .h5, .jnirs, .bnirs and .mat files');
5763
end
5864
end
65+
66+
% force adding index 1 to the group name for singular struct and cell
67+
function newroot=forceindex(root,name)
68+
newroot=root;
69+
fields=fieldnames(newroot);
70+
idx=find(ismember(fields,name));
71+
if(~isempty(idx) && length(newroot.(name))==1)
72+
newroot.(sprintf('%s1',name))=newroot.(name);
73+
newroot=rmfield(newroot,name);
74+
fields{idx(1)}=sprintf('%s1',name);
75+
newroot=orderfields(newroot,fields);
76+
end
77+

0 commit comments

Comments
 (0)