Accessing Data from .mat (version 8.1) structure in Python
Question: I have a Matlab (.mat, version >7.3) file that contains a structure (data)
that itself contains many fields. Each field is a single column array. Each
field represents an individual sensor and the array is the time series data. I
am trying to open this file in Python to do some more analysis. I am using
PyTables to read the data in:

    
    
    import tables
    impdat = tables.openFile('data_file.mat')
    

This reads the file in and I can enter the fileObject and get the names of
each field by using:

    
    
    impdat.root.data.__members__
    

This prints a list of the fields:

> ['rdg', 'freqlabels', 'freqbinsctr',... ]

Now, what I would like is a method to take each field in data and make a
python variable (perhaps dictionary) with the field name as the key (if it is
a dictionary) and the corresponding array as its value. I can see the size of
the array by doing, for example:

    
    
    impdat.root.data.rdg
    

which returns this:

    
    
    /data/rdg (EArray(1, 1286920), zlib(3))
    atom := Int32Atom(shape=(), dflt=0)
    maindim := 0
    flavor := 'numpy'
    byteorder := 'little'
    chunkshape := (1, 16290)
    

My question is how do I access some of the data stored in that large array (1,
1286920). How can I read that array into another Python variable (list,
dictionary, numpy array, etc.)? Any thoughts or guidance would be appreciated.


Answer: I have come up with a working solution. It is not very elegant as it requires
an eval. So I first create a new variable (alldata) to the data I want to
access, and then I create an empty dictionary datastruct, then I loop over all
the members of data and assign the arrays to the appropriate key in the
dictionary:

    
    
    alldata = impdat.root.data
    datastruct = {}
    for names in impdat.rood.data.__members___:
        datastruct[names] = eval('alldata.' + names + '[0][:]')
    

The '[0]' could be superfluous depending on the structure of the data trying
to access. In my case the data is stored in an array of an array and I just
want the first one. If you come up with a better solution please feel free to
share.

