2

Python's SciPy package has a function that saves Python variable into MATLAB's .mat file https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html However, the documentation lacks examples, and I don't know what is the dictionary of variables it wants as an input.

Say I have a 2D NumPy array A and a 3D NumPy array B, how do I save them into a .mat file called my_arrays.mat?

Can I save multiple variables in .mat or do I need a file to each variable?

How to read .mat files into Python and how do I know to reach variables they have been loaded?

1 Answer 1

2

Examples for saving and loading

import scipy.io as sio

# dummy data
A = np.random.randint(0,10,100)
B = np.random.randint(0,10,100)

# collect arrays in dictionary
savedict = {
    'A' : A,
    'B' : B,
}

# save to disk
sio.savemat('my_arrays.mat', savedict)

#load from disk
data = sio.loadmat('my_arrays.mat')

# alternative way to load from disk:
data = {}
sio.loadmat('my_arrays.mat', mdict=data)
Sign up to request clarification or add additional context in comments.

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.