Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

MATLAB での Python tuple 変数の使用

この例では、MATLAB® で Python® の tuple 変数を使用する方法を説明します。

tuple 入力引数を取る Python 関数の呼び出し

Python Version 2.7 の関数 cmp(a,b) は、2 つの tuple 値を比較します。cmp を呼び出すには、MATLAB の cell 配列を渡すか、py.tuple コマンドを呼び出して tuple を作成します。

tuple 変数を作成し、Python 関数に渡します。

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

    ('Robert', 19.0, 'Biology')

等価の cell 配列を作成します。

mStudent = {"Robert",19,"Biology"}
mStudent=1×3 cell array
    {["Robert"]}    {[19]}    {["Biology"]}

tuple 値を MATLAB cell 配列の値と比較します。出力は、a<b の場合は -1a=b の場合は 0a>b の場合は 1 です。値はいずれも等価です。

pe = pyenv;
if pe.Version == "2.7"
    py.cmp(pStudent, mStudent)
end

tuple から MATLAB 変数への変換

tuple を MATLAB cell 配列に変換するには、関数 cell を呼び出します。

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

tuple の要素の読み取り

MATLAB のインデックスを使用して、tuple の要素を表示します。たとえば、pStudent の最初の 2 つの要素を表示します。MATLAB は tuple 変数を返します。

pStudent(1:2)
ans = 
  Python tuple with no properties.

    ('Robert', 19.0)

1 つの要素を表示します。MATLAB は Python データ型要素を返します。

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

    Biology

単一の要素をもつ tuple の作成

単一の要素をもつ tuple 変数を作成します。MATLAB は、要素が 1 つの tuple に対しては末尾にコンマを表示します。

subject = py.tuple({'Biology'})
subject = 
  Python tuple with no properties.

    ('Biology',)