Skip to content

Commit

Permalink
support octave to load hdf5 file
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Nov 27, 2023
1 parent 3c8dc14 commit 830bf24
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
17 changes: 10 additions & 7 deletions loadh5.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
path=varargin{1};
end

opt.dotranspose=jsonopt('Transpose',1,opt);

if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
[varargout{1:nargout}]=load(filename, '-hdf5');
if(opt.dotranspose)
varargout{1}=transposemat(varargout{1});
end
return;
end

if(isa(filename,'H5ML.id'))
loc=filename;
else
Expand All @@ -77,13 +87,6 @@
opt.complexformat={'Real','Imag'};
end

opt.dotranspose=jsonopt('Transpose',1,opt);

if(exist('OCTAVE_VERSION', 'builtin') ~= 0)
[varargout{1:nargout}]=load(filename, '-hdf5');
return;
end

opt.releaseid=0;
vers=ver('MATLAB');
if(~isempty(vers))
Expand Down
40 changes: 40 additions & 0 deletions transposemat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function data=transposemat(input)
%
% data=transposemat(input)
%
% Iterate over struct/cell and transpose 2D or higher-dimensional numerical
% array to match Octave loaded HDF5 array elements with loadh5 default setting
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% name: a matlab variable, can be a cell, struct, containers.Map, numeric array or strings
%
% output:
% newname: the restored original string
%
% example:
% a=struct('a', ones(2,3), 'b', 'a string', 'c', uint8(zeros(2,3,4)));
% b=transposemat(a)
%
% this file is part of EasyH5 Toolbox: https://github.com/NeuroJSON/easyh5
%
% License: GPLv3 or 3-clause BSD license, see https://github.com/NeuroJSON/easyh5 for details
%

if(isstruct(input))
data=structfun(@transposemat, input, 'UniformOutput',false);
elseif(iscell(input))
data=cellfun(@transposemat, input, 'UniformOutput', 'false');
elseif(isa(input, 'containers.Map'))
allkeys=keys(input);
for i = 1:length(allkeys)
input(allkeys(i))=transposemat(allkeys(i));
end
elseif(isnumeric(input) && (ndims(input)>2 || all(size(input)>1)))
data=sub_data=permute(input,ndims(input):-1:1);
elseif(ischar(input) && ndims(input)==2 && size(input,1)==1 && size(input,2)>1 && input(end)==' ')
data=input(1:end-1);
else
data=input;
end

0 comments on commit 830bf24

Please sign in to comment.