Reputation: 1345
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.
Upvotes: 5
Views: 20658
Reputation: 9
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).
Upvotes: 0
Reputation: 76297
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')
Upvotes: 6