Python Forum
read matlab data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: read matlab data (/thread-40875.html)



read matlab data - pz16 - Oct-06-2023

Hello,
I have some data stored in a matab file (data.mat). The data is stored in a table as shown in the following figure (screen shot from matlab).
[attachment=2593]
I use the following python code to read in the mat-file:
import scipy.io as spio
lib = spio.loadmat('data.mat')
lib
Output:
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri Oct 6 18:07:57 2023', '__version__': '1.0', '__globals__': [], 'None': MatlabOpaque([(b'data_test', b'MCOS', b'table', array([[3707764736], [ 2], [ 1], [ 1], [ 1], [ 2]], dtype=uint32)) ], dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')]), '__function_workspace__': array([[ 0, 1, 73, ..., 0, 0, 0]], dtype=uint8)}
data_1=lib['__function_workspace__'][0]
data_1
Output:
array([ 0, 1, 73, ..., 0, 0, 0], dtype=uint8)
len(data_1)
Output:
16056
When I read in the mat-file, I get a single vector, which makes no sense to me. Does anyone have an example of how to read in matlab files? I have attached table in the zip-file.

The mat-file is stored with Matlab version 9.7.0.1190202 (R2019b)


RE: read matlab data - snippsat - Oct-06-2023

Some advice,i not to familiar with read Matlab file into Python.
Quote:To extract meaningful data from the MatlabOpaque object (table), you'd typically have a few options:

Using MATLAB: You can load the .mat file in MATLAB, convert the table to a simpler data structure (e.g., a matrix or an array),
and save it again as a new .mat file.
Then, you can load this new file in Python, and it should be more accessible.

Third-party Tools: There are some Python packages and tools developed by the community that try to handle MatlabOpaque objects.
One such tool is mat4py. You can try using it to see if it decodes your specific .mat file better.

Octave: Although Octave can handle .mat files, it might not handle the newer MATLAB OOP-style objects perfectly.
However, it's worth a try if MATLAB isn't accessible.