Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load matlab tables in python using scipy.io.loadmat

Tags:

python

matlab

Is it possible to load matlab tables in python using scipy.io.loadmat?

What I'm doing:

In Matlab:

tab = table((1:500)')
save('tab.mat', 'tab')

In Python:

import scipy.io
mat = scipy.io.loadmat('m:/tab.mat')

But I cannot access the table tab in Python using mat['tab']

like image 458
Ivan Avatar asked Sep 15 '14 17:09

Ivan


People also ask

How do I convert MATLAB data to Python?

If you collect data with Matlab but want to work on it using Python (e.g. making nice graphs with matplotlib) you can export a . mat file and then import that into Python using SciPy. Remember that in Python indexing starts at 0, rather than 1 (which is how Matlab does it).

Can you load mat files in Python?

By default, Python is not capable of reading . mat files.

How do I open a .MAT file without MATLAB?

It is not possible to open it with a text editor (except you have a special plugin as Dennis Jaheruddin says). Otherwise you will have to convert it into a text file (csv for example) with a script. This could be done by python for example: Read . mat files in Python.

What does SciPy Io do?

SciPy has many modules, classes, and functions available to read data from and write data to a variety of file formats.


2 Answers

The answer to your question is no. Many matlab objects can be loaded in python. Tables, among others, can not be loaded. See Handle Data Returned from MATLAB to Python

like image 131
Verena Haunschmid Avatar answered Sep 17 '22 08:09

Verena Haunschmid


The loadmat function doesn't load MATLAB tables. Instead a small workaround can be done. The tables can be saves as .csv files which can then be read using pandas.

In MATLAB

writetable(table_name, file_name)

In Python

df = pd.read_csv(file_name)

At the end, the DataFrame df will have the contents of table_name

like image 39
sotmot Avatar answered Sep 21 '22 08:09

sotmot