Time for action – saving and loading a .mat file
If we start with NumPy arrays and decide to use said arrays within a MATLAB or Octave environment, the easiest thing to do is create a .mat
file. We can, then, load the file within MATLAB or Octave. Let's go through the necessary steps:
- Create a NumPy array and call the
savemat()
function to create a.mat
file. This function has two parameters: a file name 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":...