Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-created HDF5 dataset transposed in Matlab

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
like image 495
John Manak Avatar asked Feb 07 '14 10:02

John Manak


People also ask

Can Matlab read HDF5?

MATLAB supports reading and writing HDF5 data sets using dynamically loaded filters.

What is HDF in Python?

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.


1 Answers

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)';
like image 121
Guillaume Jacquenot Avatar answered Sep 27 '22 21:09

Guillaume Jacquenot