How to transfer variables from Matlab to Python

Question:

I would like to transfer some of variables calculated in Matlab (R2022a) to Python environment (3.10).

I have figured out that the SciPy package has function loadmat() and I may save vars in mat file and then I should read the file with Python. However, I’m not able to use the function.

import scipy
scipy.loadmat('myVars.mat')

The interpreter argues that there is no such function as loadmat().

Thanks in advance!

Asked By: Gilad Nor

||

Answers:

You are almost at home. The function loadmat()' is in a sub-package scipy.io’. IO means input/output.
You should import and use it in that way:

import scipy.io
var_from_mat = scipy.io.loadmat('myVars.mat')

For the future, remember to check if the function is defined in the main package or in some of the sub-packages.

Answered By: Gwardi

You are importing the wrong package, check the docs.

import scipy.io as sio

mat_file = sio.loadmat('myVars.mat')
Answered By: alexrogo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.