While NumPy offers methods to store data in formats readable by Python, SciPy also provides interoperability with MATLAB. The scipy.io
module in SciPy includes functions for working with MATLAB arrays.
The savemat()
function enables exporting data in MATLAB format.
It accepts the following parameters:
filename
: The name of the file to save the data to.mdict
: A dictionary containing the data to be saved.do_compression
: A boolean indicating whether to compress the output (default is False
).Export the following array as a variable named “vec” to a MAT file.
from scipy import io import numpy as np arr = np.arange(10) io.savemat(‘arr.mat’, {“vec”: arr}) |
Note: The example above saves a file named “arr.mat” on your computer. To open the file, refer to the “Import Data from MATLAB Format” example below. |