How to preserve matlab struct when accessing in python?

mikeP picture mikeP · Aug 14, 2012 · Viewed 25.8k times · Source

I have a mat-file that I accessed using

from scipy import io
mat = io.loadmat('example.mat')

From matlab, example.mat contains the following struct

    >> load example.mat
    >> data1

    data1 =

            LAT: [53x1 double]
            LON: [53x1 double]
            TIME: [53x1 double]
            units: {3x1 cell}


    >> data2

    data2 = 

            LAT: [100x1 double]
            LON: [100x1 double]
            TIME: [100x1 double]
            units: {3x1 cell}

In matlab, I can access data as easy as data2.LON, etc.. It's not as trivial in python. It give me several option though like

mat.clear       mat.get         mat.iteritems   mat.keys        mat.setdefault  mat.viewitems   
mat.copy        mat.has_key     mat.iterkeys    mat.pop         mat.update      mat.viewkeys    
mat.fromkeys    mat.items       mat.itervalues  mat.popitem     mat.values      mat.viewvalues    

Is is possible to preserve the same structure in python? If not, how to best access the data? The present python code that I am using is very difficult to work with.

Thanks

Answer

mikeP picture mikeP · Aug 14, 2012

Found this tutorial about matlab struct and python

http://docs.scipy.org/doc/scipy/reference/tutorial/io.html