MATLAB and its open source alternative, Octave, are popular mathematical applications. The scipy.io
package has the savemat()
function, which allows you to store NumPy arrays in a .mat
file as a value of a Python dictionary.
Installing MATLAB or Octave is beyond the scope of this book. The Octave website has some pointers for installing at http://www.gnu.org/software/octave/download.html. Check out the See also section of this recipe, for instructions on installing SciPy, if necessary.
The complete code for this recipe is in the octave.py
file in this book's code bundle:
import numpy as np import scipy.io a = np.arange(7) scipy.io.savemat("a.mat", {"array": a})
Once you have installed MATLAB or Octave, you need to follow the subsequent steps to store NumPy arrays:
Create a NumPy array and call
savemat()
to store the array in a.mat
file. This function has two parameters—a file name and a dictionary containing variable names...