Main Content

Use Python tuple Variables in MATLAB

This example shows how to use Python® tuple variables in MATLAB®.

Convert tuple to MATLAB variable

To convert a tuple to a MATLAB cell array, call the cell function.

pStudent = py.tuple({'Robert',19,'Biology'})
pStudent = 
  Python tuple with values:

    ('Robert', 19.0, 'Biology')

    Use string, double or cell function to convert to a MATLAB array.

S = cell(pStudent)
S=1×3 cell array
    {1×6 py.str}    {[19]}    {1×7 py.str}

Read Elements in tuple

Use MATLAB indexing to display elements in a tuple. For example, display the first two elements of pStudent. MATLAB returns a tuple variable.

pStudent(1:2)
ans = 
  Python tuple with values:

    ('Robert', 19.0)

    Use string, double or cell function to convert to a MATLAB array.

Display one element. MATLAB returns a Python data type element.

pStudent{3}
ans = 
  Python str with no properties.

    Biology

Create tuple Containing Single Element

Create a tuple variable with a single element. MATLAB displays a trailing comma for a tuple with one element.

subject = py.tuple({'Biology'})
subject = 
  Python tuple with values:

    ('Biology',)

    Use string, double or cell function to convert to a MATLAB array.