Main Content

Use MATLAB Arrays in Python

This example shows how to use MATLAB® arrays in Python®.

The matlab package provides new Python data types to create arrays that can be passed to MATLAB functions. The matlab package can create arrays of any MATLAB numeric or logical type from Python sequence types. Multidimensional MATLAB arrays are supported.

Create a MATLAB array in Python, and call a MATLAB function on it.

import matlab
from production_server import client
client_obj = client.MWHttpClient("http://localhost:9910")
x = matlab.double([1,4,9,16,25])


You can use matlab.double to create an array of doubles given a Python list that contains numbers. You can call a MATLAB function such as sqrt on x, and the return value is another matlab.double array.

Create a multidimensional array. The magic function returns a 2-D array to Python scope.

a = client_obj.myArchive.magic(6)


Call the tril function to get the lower triangular portion of a.

b = client_obj.myArchive.tril(a)


Related Topics