Python での MATLAB ディクショナリの使用
Python® dict を MATLAB® ディクショナリに変換し、MATLAB ディクショナリを Python dict に変換できます。Python コードで、MATLAB ディクショナリに対して dict メソッドを使用できます。
Python
dictを MATLAB ディクショナリに変換するには、まずそれをmatlab.dictionaryコンストラクターに渡し、次に生成されたmatlab.dictionaryオブジェクトを MATLAB に渡します。あるいは、Python ディクショナリを MATLAB に直接渡すことでも、Python ディクショナリを MATLAB 構造体に変換できます。MATLAB ディクショナリを
matlab.dictionaryオブジェクトとして Python に渡すことができます。あるいは、Python でmatlab.dictionaryオブジェクトを直接作成することもできます。このオブジェクトは Pythondictのように動作します。
Python ディクショナリから MATLAB のディクショナリまたは構造体への変換
Python dict を MATLAB ディクショナリに変換するには、それを Python matlab.dictionary 関数に渡し、結果の matlab.dictionary オブジェクトを MATLAB に渡します。(R2024b 以降)
たとえば、Python で Python dict から matlab.dictionary オブジェクトを作成します。次に、workspace を使用して、matlab.dictionary オブジェクトを MATLAB に渡します。この場合、MATLAB はキーを string に変換し、値を double に変換します。
import matlab.engine eng = matlab.engine.start_matlab('-desktop') pd = {'milk': 3.50, 'bread': 2.50, 'eggs': 2.75} md = matlab.dictionary(pd) eng.workspace['md'] = md
MATLAB でディクショナリを表示します。
md
md =
dictionary (string ⟼ double) with 3 entries:
"milk" ⟼ 3.5000
"bread" ⟼ 2.5000
"eggs" ⟼ 2.7500MATLAB は、可能な限り、Python のキーと値を等価な MATLAB の型に変換します。Python ディクショナリのキーが MATLAB データ型への自動変換後、すべて同じデータ型でない場合、MATLAB はキーを cell 配列にラップします。Python ディクショナリの値についても同様です。
あるいは、workspace を使用して Python dict を MATLAB に直接渡すことで、Python dict を MATLAB 構造体に変換することもできます。この変換では、struct で説明されているように、Python ディクショナリのキーが有効な構造体フィールド名である必要があります。
eng.workspace['pd'] = pdMATLAB で構造体を表示します。
pd
pd =
struct with fields:
milk: 3.5000
bread: 2.5000
eggs: 2.7500マッピング プロトコルをサポートする Python オブジェクト タイプ (dict 以外) も MATLAB ディクショナリに変換できます。workspace を使用して、これらのオブジェクトを MATLAB に直接渡します。このようなオブジェクトは最初に matlab.dictionary オブジェクトに変換する必要はありません。たとえば、Python OrderedDict を MATLAB ディクショナリに変換します。
import collections import matlab.engine eng = matlab.engine.start_matlab('-desktop') od = collections.OrderedDict({'soup': 3.57, 'bread': 2.29, 'bacon': 3.91, 'salad': 5.00}) eng.workspace['od'] = od
MATLAB により、Python OrderedDict が MATLAB ディクショナリに変換されます。MATLAB でディクショナリを表示します。
od
od =
dictionary (string ⟼ double) with 4 entries:
"soup" ⟼ 3.5700
"bread" ⟼ 2.2900
"bacon" ⟼ 3.9100
"salad" ⟼ 5MATLAB ディクショナリから Python ディクショナリへの変換
MATLAB ディクショナリを Python に渡すと、MATLAB はディクショナリを matlab.dictionary オブジェクトとして渡します。Python ディクショナリのキーと値は、Pass Data Between MATLAB and Python from Pythonで説明されている MATLAB 型の既定の変換ルールによって決定されます。その後、dict コンストラクターを使用して、matlab.dictionary オブジェクトを Python dict に明示的に変換できます。
たとえば、MATLAB ディクショナリを作成し、それを Python dict に変換します。
まず、Python で MATLAB エンジンを起動します。
import matlab.engine eng = matlab.engine.start_matlab('-desktop')
MATLAB で MATLAB ディクショナリを作成します。
dm = dictionary(["Avg Temp","Dew Point","Precipitation"],[71.12 69.07 0.0]);
MATLAB ディクショナリを Python に渡します。作成された matlab.dictionary オブジェクトを Python dict に変換します。
dm = eng.workspace['dm']
dp = dict(dm)
Python で dict を表示します。
dp
{'Avg Temp': 71.12, 'Dew Point': 69.07, 'Precipitation': 0.0}MATLAB ディクショナリでの Python ディクショナリ メソッドの使用
Python コードで、matlab.dictionary オブジェクトに対して多くの Python dict メソッドを使用できます。
たとえば、len を使用して、matlab.dictionary オブジェクト dm の長さを出力します。
len(dm)
3
次の操作で matlab.dictionary オブジェクトがサポートされています。ここで、dm はディクショナリを表します。
list(dm)len(dm)dm[key]dm[key] = valuedel dm[key]key in dmkey not in dmiter(dm)dm.clear()dm.todict()classmethod fromkeys(iterable, value=None)dm.items()dm.keys()dm.values()dm == dm2(dm2がmatlab.dictionaryオブジェクトの場合にのみサポートされます。dm2に同じキーと値のペアが含まれている場合にのみtrueが返されます)
matlab.dictionary オブジェクトの items() メソッド、keys() メソッドおよび values() メソッドではリストが返されます。この動作は、対応するメソッドがビュー オブジェクトを返す Python dict とは異なります。