Export data from python to matlab

G=

Python ndarray with properties:

T: [1×1 py.numpy.ndarray]

base: [1×1 py.NoneType]

ctypes: [1×1 py.numpy.core._internal._ctypes]

data: [1×3 py.memoryview]

dtype: [1×1 py.numpy.dtype]

flags: [1×1 py.numpy.flagsobj]

flat: [1×1 py.numpy.flatiter]

imag: [1×1 py.numpy.ndarray]

itemsize: [1×1 py.int]

nbytes: [1×1 py.int]

ndim: [1×1 py.int]

real: [1×1 py.numpy.ndarray]

shape: [1×2 py.tuple]

size: [1×1 py.int]

strides: [1×2 py.tuple]

[[1.00000001 1.00000001 3.00000001]

[1.00000001 1.00000001 3.00000001]

[1.00000001 1.00000001 3.00000001]]

Export data from python to matlab

  • Direct link to this question

 ⋮ 

  • Direct link to this question

I have a logical and a uint16 array, both of the size 5000x2000. How can i get these two into python? E.g. into a numpy array?

Answers (1)

Export data from python to matlab

  • Direct link to this answer

 ⋮ 

  • Direct link to this answer

See Also

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Export data from python to matlab

version 1.0.1.0 (9.71 KB) by

Convert Python data structures to MATLAB data structures and vice versa.

  • Follow

  • Overview
  • Functions
  • Reviews (4)
  • Discussions (0)

With a Python distribution and MATLAB installed on the same machine, MATLAB can create Python data structures from within; e.g. myPyStr = py.str('Hello World!'). Python data structures can also be created within MATLAB by the output of a Python package. Python data can be converted back into a MATLAB data type through type-conversion, e.g. char(myPyStr). This function automates the identification of the Python data type and convert it to the appropriate MATLAB data type and vice versa.

Cite As

Kyle (2022). Core_py2matlab: convert data from Python to MATLAB and back. (https://www.mathworks.com/matlabcentral/fileexchange/53717-core_py2matlab-convert-data-from-python-to-matlab-and-back), MATLAB Central File Exchange. Retrieved September 17, 2022.

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!

Amro

123k25 gold badges237 silver badges443 bronze badges

asked Jul 7, 2009 at 22:55

0

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.

Pani

1,2791 gold badge13 silver badges20 bronze badges

answered Jul 7, 2009 at 23:13

arsars

116k22 gold badges142 silver badges134 bronze badges

2

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);

answered Jul 8, 2009 at 0:13

gnovicegnovice

125k14 gold badges255 silver badges355 bronze badges

1

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.

answered Jul 7, 2009 at 23:45

Seth JohnsonSeth Johnson

14.1k6 gold badges57 silver badges85 bronze badges

I would probably use numpy.savetxt('yourfile.mat',yourarray) in Python and then yourarray = load('yourfile.mat') in MATLAB.

Jack

10.9k13 gold badges49 silver badges65 bronze badges

answered Oct 12, 2011 at 9:03

1

You could write the matrix in Python to a CSV file and read it in MATLAB using csvread.

answered Jul 7, 2009 at 23:03

JacobJacob

33.8k14 gold badges109 silver badges164 bronze badges

1

You can also call matlab directly from python:

from mlabwrap import mlab
import numpy 
a = numpy.array([1,2,3])
mlab.plot(a)

answered Jul 30, 2011 at 18:26

SiggyFSiggyF

20.8k8 gold badges39 silver badges56 bronze badges

The toolbox npy-matlab can read *.npy binary files into MATLAB. *.npy files can be directly exported with the NumPy module. From the documentation:

>> a = rand(5,4,3);
>> writeNPY(a, 'a.npy');
>> b = readNPY('a.npy');
>> sum(a(:)==b(:))
ans =

    60

npy-matlab is a simple collection of M-files available from GitHub, with a 2-clause BSD licence.

Export data from python to matlab

Cris Luengo

51.1k7 gold badges59 silver badges113 bronze badges

answered Nov 26, 2016 at 3:36

How do I transfer data from Python to MATLAB?

Python data can be converted back into a MATLAB data type through type-conversion, e.g. char(myPyStr). This function automates the identification of the Python data type and convert it to the appropriate MATLAB data type and vice versa.

Can Python interact with MATLAB?

MATLAB® provides a flexible, two-way integration with many programming languages, including Python. This allows different teams to work together and use MATLAB algorithms within production software and IT systems.

Can you call Python from MATLAB?

To call a Python method or function, type py. followed by the module name, function name, and arguments. In most cases, MATLAB automatically converts input arguments into Python types. An exception is calling a Python function with keyword arguments.

How does MATLAB recognize Python?

To verify that Python is installed on your system, open the Python interpreter from your system prompt and call Python functions. By default, MATLAB selects the version of Python based on your system path. To view the system path in MATLAB, use the getenv('path') command.