1

I am new to python and am facing difficulty in extracting file data. So I have a .mat file which has the following output in Matlab:

d= open('tesla.mat')

d =

struct with fields:

diga: [1×1 struct]

d.diga.daten

ans =

struct with fields:

       Programmdauer: [1×78 double]
              SOCVec: [1×78 double]
            Spannung: [1×78 double]
         SpannungVec: [1×78 double]
               Strom: [1×78 double]
            StromVec: [1×78 double]
       TemperaturVec: [1×78 double]
   ThermischLeistung: [1×78 double]
ThermischLeistungVec: [1×78 double]

d.diga.daten.Spannung

ans =

Columns 1 through 20

4.0544    4.2100    4.2133    4.2149    4.2159    4.2168    4.2175    4.2181    4.2186    4.2191    4.0332    4.0265    4.0247    4.0235    4.0227    4.0223    4.0219    4.0217    4.0215    4.0213

Columns 21 through 40

4.0211    4.0210    4.0208    4.0552    4.0559    4.0561    4.0563    4.0564    4.0564    4.0564    4.0564    4.0564    4.0564    4.0564    4.0563    4.0563    4.0563    4.0563    4.0563    4.0563

Columns 41 through 60

4.0562    4.0562    4.0562    4.0562    4.0562    4.0562    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0560    4.0560    4.0560    4.0560    4.0560

Columns 61 through 78

4.0560    4.0560    4.0560    4.0560    4.0560    4.0560    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559

I want to parse this data in python and extract this specific variable e.g Spannung data . The output in python is as follows :

{'__header__': b'MATLAB 5.0 MAT-file, Platform: x86_64-pc-linux-gnu, Created by: libmatio v1.5.9 on Mon Jul  6 20:29:51 2020', '__version__': '1.0', '__globals__': [], 'diga': array([[(array([[(array([[0.        , 0.10464944, 0.21018082, 0.31937647, 0.42176473,
    0.53570583, 0.6428233 , 0.74994078, 0.85705826, 0.96417574,
    1.0681423 , 1.17389392, 1.28139943, 1.40048611, 1.52559419,
    1.64743952, 1.76928485, 1.89113018, 2.        , 2.12290829,
    2.24581658, 2.36872488, 2.49163317, 2.59454399, 2.69687044,
    2.81347033, 2.93308651, 3.05714129, 3.17464839, 3.3055228 ,
    3.45267061, 3.58849168, 3.77533445, 3.88176795, 3.998157  ,
    4.19674602, 4.34787781, 4.5       , 4.69631901, 4.83781901,
    4.99708256, 5.20507697, 5.37073747, 5.5       , 5.6080527 ,
    5.78533992, 5.95166902, 6.09878942, 6.28218433, 6.4540618 ,
    6.59066557, 6.69357254, 6.8539116 , 6.95566133, 7.08274939,
    7.19399435, 7.3374617 , 7.44876399, 7.58496883, 7.70381522,
    7.83578215, 7.94780886, 8.08803463, 8.19780798, 8.34935696,
    8.45694731, 8.58460976, 8.69666891, 8.83509531, 8.94704739,
    9.08658847, 9.20783487, 9.33469661, 9.44355668, 9.59016133,
    9.6962619 , 9.85349258, 9.95928693]]), array([[88.3       , 88.30430713, 88.30865056, 88.3131448 , 88.31735886,
    88.32204841, 88.32645712, 88.33086583, 88.33527453, 88.33968324,
    88.34053444, 88.33956722, 88.33858396, 88.33749478, 88.33635052,
    88.3352361 , 88.33412169, 88.33300727, 88.33201153, 88.33088739,
    88.32976326, 88.32863912, 88.32751498, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
    88.32743846, 88.32743846, 88.32743846]]), array([[4.05436068, 4.21002405, 4.21327229, 4.21486158, 4.21591229,
    4.2168056 , 4.21748722, 4.21807606, 4.21860772, 4.21910362,
    4.03324528, 4.0265357 , 4.02467542, 4.02350034, 4.02273807,
    4.02225604, 4.02191832, 4.02166347, 4.02147603, 4.02129491,
    4.02113115, 4.020979  , 4.02083454, 4.05516985, 4.05587307,
    4.05613862, 4.05627884, 4.05635271, 4.05638517, 4.0563976 ,
    4.0563957 , 4.05638637, 4.05636811, 4.05635644, 4.05634327,
    4.05632056, 4.05630343, 4.05628649, 4.05626516, 4.05625018,
    4.05623371, 4.05621284, 4.0561967 , 4.05618439, 4.0561743 ,
    4.05615809, 4.05614328, 4.05613048, 4.0561149 , 4.05610068,
    4.05608961, 4.05608141, 4.05606887, 4.05606105, 4.05605142,
    4.05604313, 4.05603261, 4.05602458, 4.05601491, 4.0560066 ,
    4.05599751, 4.05598991, 4.05598053, 4.0559733 , 4.05596346,
    4.05595658, 4.05594852, 4.05594153, 4.05593303, 4.05592624,
    4.05591789, 4.05591073, 4.05590333, 4.05589706, 4.05588872,
    4.05588277, 4.05587405, 4.05586825]]), array([[4.05436068, 4.21002405, 4.21327229, 4.21486158, 4.21591229,
    4.2168056 , 4.21748722, 4.21807606, 4.21860772, 4.21910362,
    4.03324528, 4.0265357 , 4.02467542, 4.02350034, 4.02273807,
    4.02225604, 4.02191832, 4.02166347, 4.02147603, 4.02129491,
    4.02113115, 4.020979  , 4.02083454, 4.05516985, 4.05587307,
    4.05613862, 4.05627884, 4.05635271, 4.05638517, 4.0563976 ,
    4.0563957 , 4.05638637, 4.05636811, 4.05635644, 4.05634327,
    4.05632056, 4.05630343, 4.05628649, 4.05626516, 4.05625018,
    4.05623371, 4.05621284, 4.0561967 , 4.05618439, 4.0561743 ,
    4.05615809, 4.05614328, 4.05613048, 4.0561149 , 4.05610068,
    4.05608961, 4.05608141, 4.05606887, 4.05606105, 4.05605142,
    4.05604313, 4.05603261, 4.05602458, 4.05601491, 4.0560066 ,
    4.05599751, 4.05598991, 4.05598053, 4.0559733 , 4.05596346,
    4.05595658, 4.05594852, 4.05594153, 4.05593303, 4.05592624,
    4.05591789, 4.05591073, 4.05590333, 4.05589706, 4.05588872,
    4.05588277, 4.05587405, 4.05586825]]), array([[ 0. ,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5, -1. ,
    -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. ,
    -1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ]]), array([[ 0. ,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5, -1. ,
    -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. ,
    -1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
     0. ]]), array([[25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
    25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
    25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
    25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
    25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
    25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.]]), array([[0.00000000e+00, 6.85992309e-01, 7.03449743e-01, 7.10867829e-01,
    7.15615588e-01, 7.19174707e-01, 7.21389575e-01, 7.22912024e-01,
    7.24007563e-01, 7.24847258e-01, 4.45033989e-02, 3.85461248e-02,
    3.58360767e-02, 3.53935152e-02, 3.56460808e-02, 3.59131697e-02,
    3.60769536e-02, 3.61500744e-02, 3.61654153e-02, 3.61491829e-02,
    3.61153062e-02, 3.60739446e-02, 3.60309650e-02, 9.69429070e-04,
    5.35032457e-04, 3.58732548e-04, 2.92579042e-04, 2.64021521e-04,
    2.49466440e-04, 2.38203495e-04, 2.27951943e-04, 2.19594624e-04,
    2.09204995e-04, 2.03745280e-04, 1.98105959e-04, 1.89197221e-04,
    1.82955847e-04, 1.77092643e-04, 1.70083570e-04, 1.65383061e-04,
    1.60410350e-04, 1.54374091e-04, 1.49900760e-04, 1.46597195e-04,
    1.43952210e-04, 1.39825410e-04, 1.36175399e-04, 1.33110819e-04,
    1.29488217e-04, 1.26275854e-04, 1.23838901e-04, 1.22066587e-04,
    1.19407152e-04, 1.17780340e-04, 1.15810875e-04, 1.14141001e-04,
    1.12057839e-04, 1.10493479e-04, 1.08637272e-04, 1.07067274e-04,
    1.05375319e-04, 1.03979386e-04, 1.02281688e-04, 1.00989373e-04,
    9.92555314e-05, 9.80583899e-05, 9.66725802e-05, 9.54858692e-05,
    9.40565860e-05, 9.29290483e-05, 9.15576202e-05, 9.03953350e-05,
    8.92071874e-05, 8.82095618e-05, 8.68967397e-05, 8.59677815e-05,
    8.46224613e-05, 8.37375088e-05]]), array([[0.00000000e+00, 6.85992309e-01, 7.03449743e-01, 7.10867829e-01,
    7.15615588e-01, 7.19174707e-01, 7.21389575e-01, 7.22912024e-01,
    7.24007563e-01, 7.24847258e-01, 4.45033989e-02, 3.85461248e-02,
    3.58360767e-02, 3.53935152e-02, 3.56460808e-02, 3.59131697e-02,
    3.60769536e-02, 3.61500744e-02, 3.61654153e-02, 3.61491829e-02,
    3.61153062e-02, 3.60739446e-02, 3.60309650e-02, 9.69429070e-04,
    5.35032457e-04, 3.58732548e-04, 2.92579042e-04, 2.64021521e-04,
    2.49466440e-04, 2.38203495e-04, 2.27951943e-04, 2.19594624e-04,
    2.09204995e-04, 2.03745280e-04, 1.98105959e-04, 1.89197221e-04,
    1.82955847e-04, 1.77092643e-04, 1.70083570e-04, 1.65383061e-04,
    1.60410350e-04, 1.54374091e-04, 1.49900760e-04, 1.46597195e-04,
    1.43952210e-04, 1.39825410e-04, 1.36175399e-04, 1.33110819e-04,
    1.29488217e-04, 1.26275854e-04, 1.23838901e-04, 1.22066587e-04,
    1.19407152e-04, 1.17780340e-04, 1.15810875e-04, 1.14141001e-04,
    1.12057839e-04, 1.10493479e-04, 1.08637272e-04, 1.07067274e-04,
    1.05375319e-04, 1.03979386e-04, 1.02281688e-04, 1.00989373e-04,
    9.92555314e-05, 9.80583899e-05, 9.66725802e-05, 9.54858692e-05,
    9.40565860e-05, 9.29290483e-05, 9.15576202e-05, 9.03953350e-05,
    8.92071874e-05, 8.82095618e-05, 8.68967397e-05, 8.59677815e-05,
    8.46224613e-05, 8.37375088e-05]]))]],
  dtype=[('Programmdauer', 'O'), ('SOCVec', 'O'), ('Spannung', 'O'), ('SpannungVec', 'O'), ('Strom', 'O'), ('StromVec', 'O'), ('TemperaturVec', 'O'), ('ThermischLeistung', 'O'), ('ThermischLeistungVec', 'O')]),)]],
  dtype=[('daten', 'O')])}

what would be the code to write in python to extract specific array data .

2 Answers 2

1

Loading matlab structs into python is a bit of a mess but the scipy.io.loadmat method can do it with a bit of exploration.

See help for scipy.io.loadmat. They explain how it works.

Start by loading the file:

import scipy.io as sio
my_struct = sio.loadmat(file_name)

Then, get the data from the dict:

my_struct.keys() # this will show you the var name of your data, in your case diga (if I understand correctly)
data = my_struct["diga"]

Now, for each level of nested structs you can use .dtype to see the next structs inside and then pull the data:

data.dtype
data[0,0]["daten"]

And then:

data[0,0,]["daten"][0,0]["Spannung"]

** I might have missed a level in your struct, play with it to see if it's not exactly fitting your struct.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u so much ... I get my desired output by this :)
1

OK I'm adding a second answer with a generalized little function that can extract data mat files with any type of fields inside, for people who may need it in the future (and even for myself, I am dealing with this question often and each time I build an ad-hoc solution...).

This function should be able to take in any mat file with structs and nested structs inside, and return a dictionary:

import scipy.io as sio

def load_from_mat(filename=None, data={}, loaded=None):
    if filename:
        vrs = sio.whosmat(filename)
        name = vrs[0][0]
        loaded = sio.loadmat(filename,struct_as_record=True)
        loaded = loaded[name]
    whats_inside = loaded.dtype.fields
    fields = list(whats_inside.keys())
    for field in fields:
        if len(loaded[0,0][field].dtype) > 0: # it's a struct
            data[field] = {}
            data[field] = load_from_mat(data=data[field], loaded=loaded[0,0][field])
        else: # it's a variable
            data[field] = loaded[0,0][field]
    return data

# and then just call the function
my_file = r"C:\Users\.......\data.mat"
data = load_from_mat(filename=my_file) # Don't worry about the other input vars (data, loaded), there are used in the recursion.    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.