python-scipyHow do I load a MAT file using Python and SciPy?
To load a MAT file using Python and SciPy, use the scipy.io.loadmat
function. This function takes the file path as an argument and returns a dictionary.
import scipy.io
mat_file = scipy.io.loadmat('example.mat')
The returned dictionary contains the variables stored in the MAT file. The keys of the dictionary are the variable names and the values are the corresponding arrays.
print(mat_file)
# {'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Thu Jan 16 14:14:17 2020',
# '__version__': '1.0',
# '__globals__': [],
# 'example_array': array([[1, 2, 3],
# [4, 5, 6]])}
The above example returns a dictionary with four keys. __header__
contains the MATLAB version and the creation date. __version__
contains the version of the MAT file. __globals__
is an empty list. The last key example_array
contains a 2x3 array.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python and SciPy to visualize data?
- How do I create a numpy array of zeros using Python?
- How do I use the numpy vstack function in Python?
- How do I use the NumPy transpose function in Python?
- How can I use Python and SciPy to find the zeros of a function?
- How do I uninstall Python Scipy?
See more codes...