Exchanging data with MATLAB and Octave
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 dictionary.
Getting ready
Installing MATLAB or Octave is outside of the scope of this book. The Octave website has some pointers for installing: http://www.gnu.org/software/octave/download.html. Check the See Also section of this recipe, for instructions on installing SciPy, if necessary.
How to do it...
Once you have installed MATLAB or Octave, you need to follow the subsequent steps to store NumPy arrays:
Call
savemat
.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 and values.a = numpy.arange(7) scipy.io.savemat("a.mat", {"array": a})
Load the
.mat
file.Navigate to the directory where you created the file. Load the file, and check the array...