Dr. Arne JachensDr. Arne Jachens

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

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

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