MATLAB Examples

Index into Python List

This example shows how to display the last element in a Python® list variable. The example compares indexing into a MATLAB® cell array with indexing into a Python list.

Create a MATLAB cell array and display the last element. MATLAB returns a cell array.

C = {1,2,3,4};
n = C(end)
n =

  cell

    [4]

Display the contents of the last element.

n = C{end}
n =

     4

Convert the cell array to a Python list.

li = py.list(C)
li = 

  Python list with no properties.

    [1.0, 2.0, 3.0, 4.0]

Display the last element. MATLAB returns a list.

n = li(end)
n = 

  Python list with no properties.

    [4.0]

Display the contents of the last element.

n = li{end}
n =

     4