diff --git a/DataFiles/FileLoadSaveClass.m b/DataFiles/FileLoadSaveClass.m index ebb3ff5..bd536ed 100644 --- a/DataFiles/FileLoadSaveClass.m +++ b/DataFiles/FileLoadSaveClass.m @@ -23,7 +23,7 @@ % --------------------------------------------------------- - function Load(obj, filename, format) + function varargout=Load(obj, filename, format) if ~exist('filename','var') filename = obj.filename; end @@ -41,9 +41,28 @@ function Load(obj, filename, format) end case obj.supportedFomats.hdf5 if ismethod(obj, 'LoadHdf5') - obj.LoadHdf5(filename); + + flds=ListHDF5fields(filename); + cnt=1; parent={}; + for i=1:length(flds) + f=strsplit(flds{i},'/'); + if(length(f)>1 && ~isempty(strfind(f{2},'nirs'))) + parent{cnt}=['/' f{2}]; + cnt=cnt+1; + end + end + parent=unique(parent); + for i=1:length(parent) + obj(i)=obj(1); + obj(i).LoadHdf5(filename,parent{i}); + end end end + if(nargout>0) + varargout={obj}; + elseif(length(parent)>1) + warning(['multiple /nirs entries found; use snirf=snirf.load(''' filename ''');']); + end end diff --git a/DataFiles/Hdf5/hd5read_safeStrArray.m b/DataFiles/Hdf5/hd5read_safeStrArray.m new file mode 100644 index 0000000..7ca5503 --- /dev/null +++ b/DataFiles/Hdf5/hd5read_safeStrArray.m @@ -0,0 +1,33 @@ +function valout2 = hd5read_safeStrArray(fname, name, val) + + valout2 = {}; +try + try + valout2 = h5read(fname, name); + catch + cnt=1; + valout={}; + while(1) + try + valout{cnt}= h5read(fname, [name num2str(cnt)]); + cnt=cnt+1; + catch + break; + end + end + + + for ii=1:length(valout) + valout2{ii,1} = convertH5StrToStr(valout{ii}); + end + end +catch + switch(class(val)) + case 'char' + valout2 = ''; + case 'cell' + valout2 = {}; + otherwise + valout2 = []; + end +end diff --git a/Snirf/DataClass.m b/Snirf/DataClass.m index 6eed697..162018e 100644 --- a/Snirf/DataClass.m +++ b/Snirf/DataClass.m @@ -105,9 +105,9 @@ obj.time = h5read(fname, [parent, '/time']); ii=1; while 1 - if ii > length(obj.measurementList) - obj.measurementList(ii) = MeasListClass; - end + if ii > length(obj.measurementList) + obj.measurementList(ii) = MeasListClass; + end if obj.measurementList(ii).LoadHdf5(fname, [parent, '/measurementList', num2str(ii)]) < 0 obj.measurementList(ii).delete(); obj.measurementList(ii) = []; diff --git a/Snirf/ListHDF5fields.m b/Snirf/ListHDF5fields.m new file mode 100644 index 0000000..9ebfcb1 --- /dev/null +++ b/Snirf/ListHDF5fields.m @@ -0,0 +1,56 @@ +function [names,val] = ListHDF5fields(filename); + +% Ted Added 4/8/2020 +info=hdf5info(filename); + +names={}; +for i=1:length(info.GroupHierarchy) + for j=1:length(info.GroupHierarchy.Groups) + n=getgroups(info.GroupHierarchy.Groups(j)); + names={names{:} n{:}}; + end + for j=1:length(info.GroupHierarchy.Datasets) + n=getdatasets(info.GroupHierarchy.Datasets(j)); + names={names{:} n{:}}; + end +end + +names=unique(names)'; + + + +if(nargout>1) + for i=1:length(names) + val{i}=hdf5read(filename,names{i}); + end + varargout{1}=val; +end + +function names= getgroups(in) + +names={}; +if(~isempty(in)) + % names={names{:} in.Name}; + for j=1:length(in.Groups) + n=getgroups(in.Groups(j)); + names={names{:} n{:}}; + end + for j=1:length(in.Datasets) + n=getdatasets(in.Datasets(j)); + names={names{:} n{:}}; + end +end + + +function names= getdatasets(in) + +names={}; +if(~isempty(in)) + names={names{:} in.Name}; + % for j=1:length(in.Groups) + % names={names{:} getgroups(in.Groups(j))}; + % end + % for j=1:length(in.Datasets) + % names={names{:} getdatasets(in.Datasets(j))}; + % end +end \ No newline at end of file diff --git a/Snirf/MeasListClass.m b/Snirf/MeasListClass.m index 3857b9a..c81da8b 100644 --- a/Snirf/MeasListClass.m +++ b/Snirf/MeasListClass.m @@ -113,7 +113,7 @@ obj.detectorIndex = hdf5read(fname, [parent, '/detectorIndex']); obj.wavelengthIndex = hdf5read(fname, [parent, '/wavelengthIndex']); obj.dataType = hdf5read(fname, [parent, '/dataType']); - obj.dataTypeLabel = convertH5StrToStr(hdf5read_safe(fname, [parent, '/dataTypeLabel'], obj.dataTypeLabel)); + obj.dataTypeLabel = convertH5StrToStr(h5read_safe(fname, [parent, '/dataTypeLabel'], obj.dataTypeLabel)); obj.dataTypeIndex = hdf5read(fname, [parent, '/dataTypeIndex']); obj.sourcePower = hdf5read_safe(fname, [parent, '/sourcePower'], obj.sourcePower); obj.detectorGain = hdf5read_safe(fname, [parent, '/detectorGain'], obj.detectorGain); diff --git a/Snirf/ProbeClass.m b/Snirf/ProbeClass.m index fb03e89..ee33dae 100644 --- a/Snirf/ProbeClass.m +++ b/Snirf/ProbeClass.m @@ -100,14 +100,10 @@ obj.momentOrder = hdf5read_safe(fname, [parent, '/momentOrder'], obj.momentOrder); obj.correlationTimeDelay = hdf5read_safe(fname, [parent, '/correlationTimeDelay'], obj.correlationTimeDelay); obj.correlationTimeDelayWidth = hdf5read_safe(fname, [parent, '/correlationTimeDelayWidth'], obj.correlationTimeDelayWidth); - obj.sourceLabels = h5read_safe(fname, [parent, '/sourceLabels'], obj.sourceLabels); - for ii=1:length(obj.sourceLabels) - obj.sourceLabels{ii} = convertH5StrToStr(obj.sourceLabels{ii}); - end - obj.detectorLabels = h5read_safe(fname, [parent, '/detectorLabels'], obj.detectorLabels); - for ii=1:length(obj.detectorLabels) - obj.detectorLabels{ii} = convertH5StrToStr(obj.detectorLabels{ii}); - end + obj.sourceLabels = hd5read_safeStrArray(fname, [parent, '/sourceLabels'], obj.sourceLabels); + + obj.detectorLabels = hd5read_safeStrArray(fname, [parent, '/detectorLabels'], obj.detectorLabels); + catch err=-1; return;