Use double function to convert to a MATLAB array. Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
If you collect data with Matlab but want to work on it using Python (e.g. making nice graphs with matplotlib) you can export a . mat file and then import that into Python using SciPy. Remember that in Python indexing starts at 0, rather than 1 (which is how Matlab does it).
NumPy (Numerical Python)NumPy arrays are the equivalent to the basic array data structure in MATLAB. With NumPy arrays, you can do things like inner and outer products, transposition, and element-wise operations.
If you use numpy/scipy, you can use the scipy.io.savemat
function:
import numpy, scipy.io
arr = numpy.arange(9) # 1d array of 9 numbers
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.
I think ars has the most straight-forward answer for saving the data to a .mat file from Python (using savemat). To add just a little to their answer, you can also load the .mat file into MATLAB programmatically using the LOAD function instead of doing it by hand using the MATLAB command window menu...
You can use either the command syntax form of LOAD:
load c:/tmp/arrdata.mat
or the function syntax form (if you have the file path stored in a string):
filePath = 'c:/tmp/arrdata.mat';
data = load(filePath);
I wrote a small function to do this same thing, without need for numpy. It takes a list of lists and returns a string with a MATLAB-formatted matrix.
def arrayOfArrayToMatlabString(array):
return '[' + "\n ".join(" ".join("%6g" % val for val in line) for line in array) + ']'
Write "myMatrix = " + arrayOfArrayToMatlabString(array)
to a .m
file, open it in matlab, and execute it.
I would probably use numpy.savetxt('yourfile.mat',yourarray)
in Python
and then yourarray = load('yourfile.mat')
in MATLAB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With