简体   繁体   中英

Reading Matlab data in python with scipy.io

I am trying to read some experimental conditions from a MATLAB 5.0 file using scipy.io. The problem is the output file is a ridiculously complicated series of arrays. How can I filter through the data within the matlab file?

import scipy.io as sio
with open("sequence_output.txt", "w") as f:
    mat = sio.loadmat("seq_data.seq")
    f.write(str(mat))

This gives me something like the below in the output file. (The actual file is > 800 lines).

How can I pick out the data I need from this file?

{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Thu Mar 15 13:50:48 2018', '__version__': '1.0', '__globals__': [], 'StepData': array([[(array([[1]], dtype=uint8), array(['LoadPlate'], dtype='<U9'), array([[1]], dtype=uint8), array([[1]], dtype=uint8), array([[0]], dtype=uint8), array([[0]], dtype=uint8), array([[(array([[12]], dtype=uint8), array([[8]], dtype=uint8), array([[0]], dtype=uint8), array([[1]], dtype=uint8), array([[60]], dtype=uint8), array([[1]], dtype=uint8), array([[1]], dtype=uint8), array([[(array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),

mat['StepData'] appears to a be a 2d array, containing one string dtype array, and whole bunch of uint8 arrays (all being (1,1) arrays). shape of this array may be (1,n) but the display is incomplete. dtype is object since it contain arrays as elements. The MATLAB source was probably a cell .

I can't copy-n-paste your comment to further decipher it, but it too looks like a mix of string arrays and uint8 ones.

MATLAB matrices are loaded as numpy numeric or string arrays. They will always be 2d (or higher), and may be transposed (or order='F').

MATLAB cells can have a mix of contents (numeric, string, etc) and are loaded as object dtype arrays.

MATLAB struct are loaded as structured arrays, with field names corresponding to the struct fields/attributes.

Search on '[scipy] loadmat' to see other questions about using loadmat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM