5

I tried to import and read .mat file from Python. I have tried two ways but been unsuccessful.

Method 1 (In Python):

import scipy.io as sio    
mat = sio.loadmat('path/tmpPBworkspace.mat')

I get a message similar to:

{'None': MatlabOpaque([ (b'rateQualityOutTrim', b'MCOS', b'dataset', array([[3707764736],
        [         2],
        [         1],
        [         1],
        [         1],
        [         1]], dtype=uint32))],
              dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')]),
 '__function_workspace__': array([[ 0,  1, 73, ...,  0,  0,  0]], dtype=uint8),
 '__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Thu May 10 07:11:52 2018',
 '__version__': '1.0'}

I am not sure what went wrong there? I was hoping to see a data frame. Also to add, in Method 1, I have saved the .mat in a version compatible with SciPy.

In Matlab:

save('path/tmpPBworkspace.mat','rateQualityOutTrim','-v7')

Also tried the other way:

Method 2: h5py

In Matlab:

save('path/tmpPBworkspaceH5.mat','rateQualityOutTrim','-v7.3')

In Python:

import numpy as np
import h5py
f = h5py.File('/GAAR/ustr/projects/PBF/tmpPBworkspaceH5.mat','r')
data = f.get('rateQualityOutTrim/date')
data = np.array(data)

I get

f
Out[154]: <HDF5 file "tmpPBworkspaceH5.mat" (mode r)>

data
array(None, dtype=object)

The array is empty. Not sure how I can access the data here as well.

9
  • 1
    The Opaque item is a matlab class object that it can't turn into a numpy array. Commented May 11, 2018 at 9:43
  • thanks hpaulj any idea on how I can read .mat ? Commented May 11, 2018 at 10:07
  • what is the matlab object? Commented May 11, 2018 at 11:56
  • what do you mean? Commented May 11, 2018 at 12:52
  • I don't about datasets in MATLAB or whether they are compatible with pandas. But to load variables with loadmat, you have to write matrices, cells, or structs. Commented May 11, 2018 at 15:48

2 Answers 2

6

You can use scipy.io.loadmat for this:

from scipy import io

loaded = io.loadmat('/GAAR/ustr/projects/PBF/tmpPBworkspaceH5.mat')

loaded will be a dictionary mapping names to arrays.


If you're in control of both the Matlab part and the Pandas part, however, it is much easier to use csvwrite:

In Matlab:

csvwrite('path/tmpPBworkspaceH5.csv','rateQualityOutTrim')

In Python:

pd.read_csv('tmpPBworkspaceH5.csv')
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Ami Tavory. I did that and I get similar message as before {'None': MatlabOpaque([ (b'rateQualityOutTrim', b'MCOS', b'dataset', array([[3707764736], [ 2], [ 1], [ 1], [ 1], [ 1]], dtype=uint32))], dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')]), '__function_workspace__': array([[ 0, 1, 73, ..., 0, 0, 0]], dtype=uint8), '__globals__': [], '__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri May 11 03:33:35 2018', '__version__': '1.0'}
still not sure how i can extract the data?
@SBad Got it - this is explained very nicely in this notebook - it's in Julia, but you can follow the explanations.
@SBad Incidentally, looking at your question, it looks like you're in control of the Matlab part as well. In this case, there are much easier options. I edited my answer to include one.
@SBad It's a long answer, since this format is really not meant to be used for exporting - it's reverse engineered, and you probably don't want to write in it to the first place.
|
0

I also would try it with scipy.io.

I have a Matlab "struct" (Auslage_000.mat) that I understand as some sort of nested dictionary. It has several header information and three data channels (vibration data). I also find Spyder (Python Development Environment) helpful as once the data is loaded you can access the data via a variable manager (similar to Matlab).

import scipy.io as sio
    
mat_contents = sio.loadmat('Auslage_000.mat',squeeze_me=True,struct_as_record=False)

When I check the output of my variable "mat_contends" I get

mat_contents

Out[14]: 
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on 2019-08-14 13:14:56 by TiePie software (www.tiepie.com).',
 '__version__': '1.0',
 '__globals__': [],
 'tpd': <scipy.io.matlab.mio5_params.mat_struct at 0x1ea3441d438>}

My actual data is in tpd. I can further access the data as follows:

#Access the data via the key 'tpd' and then the attribute 'Data'
# -> Data is a numpy array with 3 channels (ch1, ch2, ch3) / dimensions
Data = mat_contents['tpd'].Data
    
# extract channel1 
    
ch1 = Data[0]

I guess you have to dig a little bit as first you have "keys" and the "attributes" in your Matlab file (if it is a struct).

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.