2

I am trying to import a MATLAB (.mat -7.3) file in Python 3.8 using the h5py module. The file contains a structure class and table class. I successfully imported the structure class object. However, the table class is showing wrong dimensions after importing.

    import h5py
    
    Path='data/LUT_0/LUT_0.mat' #path file path to be read
    f = h5py.File(Path, mode='r') #read mat file

    list(f.keys())

Results in:

['#refs#', '#subsystem#', 'LUT_Refl', 'LUT_Var']

LUT_Var is a <HDF5 dataset "LUT_Var": shape(1,6), type"">. Trying to access the data results in:

f['LUT_Var'][()]

array([[3707764736,          2,          1,          1,          1,
                 1]], dtype=uint32)

However, I am expecting a table of the size: 169560x12. When I export this table as a txt file from MATLAB, I can import is just fine in Python. I can also re-import the .mat file in MATLAB and don´t see any corruption of the data. Does anyone know what could be missing here?

Thanks.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Rene
  • 41
  • 6
  • MATLAB's h5 library version is so obsolete that one can not even obtain it. I suggest using [`scypy.io.loadmat`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html) . – Ander Biguri Jul 28 '20 at 14:36
  • 2
    Thanks, Ander. I tried that. "The following error was raised: NotImplementedError: Please use HDF reader for matlab v7.3 files" This led me to other posts here -> https://stackoverflow.com/questions/17316880/reading-v-7-3-mat-file-in-python But I never managed to solve my problem. Again, importing the structure part of the .mat worked well. – Rene Jul 28 '20 at 15:30
  • 3
    I'm having the same issue. By the way, I get the same *exact* array as you with my table data (`array([[3707764736, 2, 1, 1, 1, 1]], dtype=uint32`) despite having a completely different table. This must be a header Matlab uses in the HDF5 format to signify a table. The contents of that table seem to be lost in h5py though. – ThatNewGuy Apr 01 '21 at 19:23
  • Linked: https://stackoverflow.com/questions/25853840/load-matlab-tables-in-python-using-scipy-io-loadmat – Heberto Mayorquin Jun 01 '22 at 23:54

1 Answers1

1

@ThatNewGuy. I could access the contents of the Matlab file this way:

import h5py
import numpy as np
import pandas as pd

f = h5py.File(matloc, 'r') # matloc = path to your .mat
print(list(f))

re = f.get('LUT_Refl/Full')
w = f.get('LUT_Refl/WL')

data = pd.DataFrame(np.array(re)).transpose()  # For converting to a NumPy array
wl = pd.DataFrame(np.array(w))
Rene
  • 41
  • 6
  • Unfortunately this does not work in the general case. See also https://github.com/skjerns/mat7.3/issues/32 – skjerns Mar 15 '22 at 06:28