I have some data that I share between Python and Matlab. I used to do it by saving NumPy arrays in MATLAB-style .mat files but would like to switch to HDF5 datasets. However, I've noticed a funny feature: when I save a NumPy array in an HDF5 file (using h5py) and then read it in Matlab (using h5read), it ends up being transposed. Is there something I'm missing?
Python code:
import numpy as np
import h5py
mystuff = np.random.rand(10,30)
f = h5py.File('/home/user/test.h5', 'w')
f['mydataset'] = mystuff
f.close()
Matlab code:
mystuff = h5read('/home/user/test.h5', '/mydataset');
size(mystuff) % 30 by 10
MATLAB supports reading and writing HDF5 data sets using dynamically loaded filters.
HDF5 file stands for Hierarchical Data Format 5. It is an open-source file which comes in handy to store large amount of data. As the name suggests, it stores data in a hierarchical structure within a single file.
When reading data from MatLab, dimensions of the data read need to be permuted to retrieve data layout. To do so, permute function is used. The code below gives the general case with any number of dimensions
rawdata = h5read(h5Filename,h5Dataset);
ndim = numel(size(rawdata));
data = permute(rawdata,[ndim:-1:1]);
When one works with 2D data, one can only transpose result from h5read
data = h5read(h5Filename,h5Dataset)';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With