Matrix from Python to MATLAB


Question

I'm working with Python and MATLAB right now and I have a 2D array in Python that I need to write to a file and then be able to read it into MATLAB as a matrix. Any ideas on how to do this?

Thanks!

1
40
6/15/2012 10:22:58 PM

If you use numpy/scipy, you can use the scipy.io.savemat function:

import numpy, scipy.io

arr = numpy.arange(10)
arr = arr.reshape((3, 3))  # 2d array of 3x3

scipy.io.savemat('c:/tmp/arrdata.mat', mdict={'arr': arr})

Now, you can load this data into MATLAB using File -> Load Data. Select the file and the arr variable (a 3x3 matrix) will be available in your environment.

Note: I did this on scipy 0.7.0. (scipy 0.6 has savemat in the scipy.io.mio module.) See the latest documentation for more detail

EDIT: updated link thanks to @gnovice.

65
5/23/2017 12:18:15 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon