Importing MATLAB files

Introduction to Importing Data in Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

MATLAB

  • “Matrix Laboratory”
  • Industry standard in engineering and science
  • Data saved as .mat files

ch_2_4.005.png

Introduction to Importing Data in Python

SciPy to the rescue!

  • scipy.io.loadmat() - read .mat files
  • scipy.io.savemat() - write .mat files
Introduction to Importing Data in Python

What is a .mat file?

ch_2_4.010.png

Introduction to Importing Data in Python

What is a .mat file?

ch_2_4.011.png

Introduction to Importing Data in Python

Importing a .mat file

import scipy.io
filename = 'workspace.mat'
mat = scipy.io.loadmat(filename)

print(type(mat))
<class 'dict'>
  • keys = MATLAB variable names
  • values = objects assigned to variables
print(type(mat['x']))
<class 'numpy.ndarray'>
Introduction to Importing Data in Python

Let's practice!

Introduction to Importing Data in Python

Preparing Video For Download...