Main Content

Error Converting Elements of list or tuple

You can convert Python® list and tuple types using MATLAB® string and numeric converters. For a list of MATLAB converter functions, see the py.list and py.tuple entry in the Explicitly Convert Python Types to MATLAB Types table.

Some conversion functions do not support converting list or tuple elements of different types.

For example, you can convert an array.array of double to a MATLAB double:

a = py.array.array('d', [1,2,3,4]);
double(a)
ans =

     1     2     3     4

If you put a into a list with different numeric types, the double conversion fails:

l = py.list({3.14, a});
double(l)
Error using py.list/double
Conversion of Python element at position 2 to type 'double' failed.

Use the cell function instead:

cell(l)
ans =

  1×2 cell array

    {[3.1400]}    {1×1 py.array.array}

Some conversion functions might not work on lists or tuples that contain elements which cannot be converted to the requested type:

double(py.list({3.0, 'MATLAB'}))
Error using py.list/double
Conversion of Python element at position 2 to type 'double' failed. All
Python elements must be convertible as scalar to the requested type.

Related Topics