How to read a .mat (MATLAB) file in Python

In order to read a .mat Matlab file from your Python code, you can make use of the module.

This module contains method which you can use to load a .mat file.

Example with module:


my_matlab_file ='my_matlab_file.mat')

patient = my_matlab_file['patient']
infection = my_matlab_file['infection']
treatment = my_matlab_file['treatment']


Note: To read MATLAB 7.3 file formats you will need HDF5 Python library.

Example with h5py module:

import h5py

my_matlab_file = h5py.File('my_matlab_file.mat', 'r')

data_x = my_matlab_file['data_x']
data_y = my_matlab_file['data_y']

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap