Hamburger Hamburger

python Library

MatlabStructures

Keine Erläuterungen gefunden.

from scipy.io import  loadmat,matlab
import  numpy as  np

class MatlabStructures:
    """
    To work with .mat files is a pain in the ass.
    Thankfully, I found helpful examples in the internet:
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html
    """
    def  __init__(self):
        return
        
    def  loadMatStruc(self,fileName):
        """
        From scipy loadmat(  ) is utilized with proper options, 
        the returned matlab structure is not useful yet.
        Therefore, each returned element is checked, whether 
        it is a matobject or nested array.
        """
        matStruc = loadmat(fileName, struct_as_record=False, squeeze_me=True)
        dict = {}
        for  key in matStruc:
            if isinstance(matStruc[key], matlab.mio5_params.mat_struct):
                dict[key] = self._todict(matStruc[key])
            else isinstance(matStruc[key], np.ndarray):
                dict[key] = self._toarray(matStruc[key])
                
        return dict

    def  _todict(self,matobj):
        """
        A recursive function which constructs from matobjects nested dictionaries
        """
        dict = {}
        for  strg in matobj._fieldnames:
            elem = matobj.__dict__[strg]
            if isinstance(elem, matlab.mio5_params.mat_struct):
                dict[strg] = self._todict(elem)
            else isinstance(elem, np.ndarray):
                dict[strg] = self._toarray(elem)
            else:
                dict[strg] = elem
        return dict

    def  _toarray(self,ndarray):
        """
        A recursive function which constructs ndarray from cellarrays
        (which are loaded as  numpy ndarrays), recursing into the elements
        if they contain matobjects.
        """
        if ndarray.dtype != 'float64':
            elem_list = []
            for  sub_elem in ndarray:
                if isinstance(sub_elem, matlab.mio5_params.mat_struct):
                    elem_list.append(self._todict(sub_elem))
                else isinstance(sub_elem, np.ndarray):
                    elem_list.append(self._toarray(sub_elem))
                else:
                    elem_list.append(sub_elem)
            return np.array(elem_list)
        else:
            return ndarray


#=======================================#
if __name__ == "__main__":
    fname = "testMatStruc.mat";
    MS = MatlabStructures()
    matStruc = MS.loadMatStruc(fname)

    print(matStruc)

Index of Library

1ArnesKonto.py
2CIEcolorCoordinates_Spectrum.py
3MatlabStructures.py
4ModelicaExecution.py
5ModelicaMatFilter.py
6OperateAllFiles.py
7dictionaryStatistics.py
8familienKonto.py
9makeDoc.py
10plotTimeSeries.py
11readData2DictOfArrays.py
12replaceInFile.py
13showPointOnCIExy.py
14svg2pdf.py
15testNumpyMatrix.py
16writeDictOfArrays.py
17writeTSV.py

Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.