Search code examples
pythonmatlabscipymat-file

Load matlab .mat export of signal logs in python


I am exporting signals in matlab R2023a like this:

save logs.mat logsout -v7

v7 is recomended version for reading the file in python (seen in different threads).

However, the file read by python is different from what I have in matlab. In matlab I have a nice dataset:

matlab

but after reading it in python with following code:

from scipy.io import loadmat  # this is the SciPy module that loads mat-files
mat = loadmat('logs.mat')

this is what I see:

in python

Instead of structure, I have a array of shape (1, 277355408). What do I miss? I need to process the signals in python after generating.


Solution

  • It seems, as suggested by @hpaulj - loadmat can handle matlab matrix, cell, and struct. Looks like it can't handle a dataset. I solved it by exporting variables alone like this:

    logsout{1}.Values.Data 
    save(logsout{1}.Name, 'ans', '-v7')
    

    Just an example for first signal.

    Now I can read it with scipy. However, there is also way to save it as hdf5 file and read and work with it as follows:

    import nexusformat.nexus as nx
    f = nx.nxload('logs.h5')
    prem = f.tree # this can be used to print the tree
    
    #or like this
    import h5py
    f = h5py.File('logs.h5', 'r')