MATLAB Examples

Use Python List of Numeric Types in MATLAB

This example shows how to convert a Python® list of numeric values into a MATLAB® array of double.

A Python list contains elements of any type and can contain elements of mixed types. The MATLAB double function used in this example assumes that all elements of the Python list are numeric.

Suppose that you have a Python function that returns the following list of integers P. To run this example, create the variable.

P = py.list({int32(1), int32(2), int32(3), int32(4)})
P = 

  Python list with no properties.

    [1, 2, 3, 4]

Display the numeric type of the values.

ans =

  1×6 char array

Convert P to a MATLAB cell array.

cP = cell(P);

Convert the cell array to a MATLAB array of double.

A = cellfun(@double,cP)
A =

     1     2     3     4