How to export complex-valued matrix to HDF file
Show older comments
Hello,
I need to export complex (real,imaginary) data from Matlab using the HDF5 format. I am using the Armadillo C++ template library and I need to duplicate the file structure used by Armadillo.
The HDF5 documentation is not too helpful. It is way too general for me to understand how to simply export complex data. Someone else must surely have done this before.
Below is the output from h5disp so you can see the structure of the files written by Armadillo.
>> h5disp('Example-complex.h5')
HDF5 Example-complex.h5
Group '/'
Dataset 'dataset'
Size: 4x5
MaxSize: 4x5
Datatype: H5T_COMPOUND
Member 'real': H5T_IEEE_F64LE (double)
Member 'imag': H5T_IEEE_F64LE (double)
ChunkSize: []
Filters: none
FillValue: H5T_COMPOUND
Thanks a lot for any pointers.
Accepted Answer
More Answers (2)
per isakson
on 3 Nov 2014
Edited: per isakson
on 13 Dec 2019
Datatype: H5T_COMPOUND   is not supported by the Matlab high-level HDF5 functions. The low-level functions, which interact directly with HDF5 library functions, are needed to write to this file. (I find these functions a bit tricky to handle.)
Documentation says: " Using the MATLAB Low-Level HDF5 Functions to Export Data   -   MATLAB provides direct access to dozens of functions in the HDF5 library with low-level functions that correspond to the functions in the HDF5 library. In this way, you can access the features of the HDF5 library from MATLAB, such as reading and writing complex data types and using the HDF5 sub-setting capabilities."  
However see
- Compound Data Type
- HDF5 data to struct reader by Pauli Virtanen
- HDF5 1.8 Matlab Examples   A large set of examples with the low-level functions. I think they are good.
- hdf-forum   I think that's the place to pose this question. There are some Matlab related questions.
It might be easier for you to use C++ and make a   mex-file (See: Build MEX-function from C/C++ or Fortran source code).
Added five years later in response to a comment
HDF5 still doesn't handle complex numbers natively. Thus, I assume we will not see it in Matlab anytime soon.
See the File Exchange submission EasyH5 by Qianqian Fang. Its description says: "The `saveh5.m` can handle almost all MATLAB data types, including [...] real and complex arrays" and "EasyH5 stores complex numerical arrays using a special compound data type in an HDF5 dataset."
And see the File Exchange submission HDF5TOOLS by John [Staff]. It contains a subfunction with a promising name
function write_complex_dataset(datasetId,data,offset,stride,count)
3 Comments
Matteo Seclì
on 12 Dec 2019
Edited: Matteo Seclì
on 13 Dec 2019
Hello,
I'm stuck on the same issue and just found this old topic. Did you eventually managed to sort it out? If yes, via a regular MATLAB function using the low-level API or via a MEX-function approach?
I have to do I/O of very large complex matrices, so I definitely cannot write to raw_ascii with Armadillo and then import it into MATLAB.
Thank you!
per isakson
on 12 Dec 2019
See the links to the File Exchange, which I've added to my answer.
Matteo Seclì
on 13 Dec 2019
Thanks a lot,
the two links you've sent look very promising!
Categories
Find more on HDF5 in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!