How to preserve matlab struct when accessing in python?

前端 未结 4 849
离开以前
离开以前 2021-02-08 04:41

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

相关标签:
4条回答
  • 2021-02-08 05:11

    (!) In case of nested structures saved in *.mat files, is necessary to check if the items in the dictionary that io.loadmat outputs are Matlab structures. For example if in Matlab

    >> thisStruct
    
    ans =
          var1: [1x1 struct]
          var2: 3.5
    
    >> thisStruct.var1
    
    ans =
          subvar1: [1x100 double]
          subvar2: [32x233 double]
    

    Then use the code by mergen in scipy.io.loadmat nested structures (i.e. dictionaries)

    0 讨论(0)
  • 2021-02-08 05:15

    Found this tutorial about matlab struct and python

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

    0 讨论(0)
  • 2021-02-08 05:22

    When I need to load data into Python from MATLAB that is stored in an array of structs {strut_1,struct_2} I extract a list of keys and values from the object that I load with scipy.io.loadmat. I can then assemble these into there own variables, or if needed, repackage them into a dictionary. The use of the exec command may not be appropriate in all cases, but if you are just trying to processes data it works well.

    # Load the data into Python     
    D= sio.loadmat('data.mat')
    
    # build a list of keys and values for each entry in the structure
    vals = D['results'][0,0] #<-- set the array you want to access. 
    keys = D['results'][0,0].dtype.descr
    
    # Assemble the keys and values into variables with the same name as that used in MATLAB
    for i in range(len(keys)):
        key = keys[i][0]
        val = np.squeeze(vals[key][0][0])  # squeeze is used to covert matlat (1,n) arrays into numpy (1,) arrays. 
        exec(key + '=val')
    
    0 讨论(0)
  • 2021-02-08 05:22

    this will return the mat structure as a dictionary

    def _check_keys( dict):
    """
    checks if entries in dictionary are mat-objects. If yes
    todict is called to change them to nested dictionaries
    """
    for key in dict:
        if isinstance(dict[key], sio.matlab.mio5_params.mat_struct):
            dict[key] = _todict(dict[key])
    return dict
    
    
    def _todict(matobj):
        """
        A recursive function which constructs from matobjects nested dictionaries
        """
        dict = {}
        for strg in matobj._fieldnames:
            elem = matobj.__dict__[strg]
            if isinstance(elem, sio.matlab.mio5_params.mat_struct):
                dict[strg] = _todict(elem)
            else:
                dict[strg] = elem
        return dict
    
    
    def loadmat(filename):
        """
        this function should be called instead of direct scipy.io .loadmat
        as it cures the problem of not properly recovering python dictionaries
        from mat files. It calls the function check keys to cure all entries
        which are still mat-objects
        """
        data = sio.loadmat(filename, struct_as_record=False, squeeze_me=True)
        return _check_keys(data)
    
    0 讨论(0)
提交回复
热议问题