Time for action – saving and loading a .mat file
If we start with NumPy arrays and decide to use the said arrays within a MATLAB or Octave environment, the easiest thing to do is create a .mat
file. We then can load the file within MATLAB or Octave. Let’s go through the necessary steps:
Create a NumPy array and call
savemat
to create a.mat
file. This function has two parameters – a filename and a dictionary containing variable names and values.a = np.arange(7) io.savemat(“a.mat”, {“array”: a})
Within a MATLAB or Octave environment, load the
.mat
file and check the stored array.octave-3.4.0:7> load a.mat octave-3.4.0:8> a octave-3.4.0:8> array array = 0 1 2 3 4 5 6
What just happened?
We created a .mat
file from NumPy code and loaded it within Octave. We checked the NumPy array that was created (see scipyio.py
).
import numpy as np from scipy import io a = np.arange(7) io.savemat(“a.mat”, {“array”: a})
Pop quiz – loading .mat files
Q1. Which function loads .mat
files...