メインコンテンツ

Python 関数から返されたデータの処理

Python 型から MATLAB 型への自動変換

MATLAB® は Python® 関数から返された以下のデータ型を自動的に MATLAB 型に変換します。

Python に表示される Python の戻り値の型

結果の MATLAB の型 — スカラー

float

double

complex

複素数の double

bool

logical

datetime

datetime

また、Python string オブジェクトを含む Python ディクショナリまたは Python Pandas DataFrame が Python から MATLAB に返されると、Python string オブジェクトは MATLAB string に変換されます。

Python 型から MATLAB 型への明示的な変換

以下の MATLAB 関数を使用して、Python データ型を MATLAB 型に明示的に変換できます。

MATLAB に表示される Python の戻り値の型またはプロトコル

MATLAB の変換関数

py.str

string
char

MATLAB での Python str 変数の使用

__str__ メソッドをもつオブジェクト

char

py.help('datetime.date.__str__')
Help on wrapper_descriptor in datetime.date:

datetime.date.__str__ = __str__(self, /)
    Return str(self).
d = py.datetime.date(...
    int32(2020),int32(3),int32(4));
char(d)
ans = '2020-3-04'

py.int
py.long
py.float
py.bool

double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64

 
logical

py.bytes

uint8

 

py.array.array
py.numpy.ndarray
py.memoryview

任意の形式の py.array.array および py.memoryview オブジェクトを、目的の MATLAB 型に変換できます。

double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64

MATLAB での Python 数値変数の使用

py.list および py.tuple

double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64
logical
string
cell

MATLAB での Python list 変数の使用
MATLAB での Python tuple 変数の使用

詳細については、list または tuple の要素の変換中のエラーを参照してください。

マッピング プロトコルを実装する py.dict またはその他のオブジェクト

dictionary
struct

MATLAB での Python ディクショナリの使用
py.pandas.DataFrame

table
timetable

MATLAB での Python pandas DataFrame の使用

py.datetime.datetime

datetime

dt = py.numpy.arange('2022-12-30', ...
    '2023-01-10',dtype='datetime64[D]');

py.datetime.timedelta

duration

td = py.numpy.array([1,2,3,4], ...
    dtype='timedelta64[h]');

Python 関数の出力の型が、numpy.ndarray などの Python バッファー プロトコルを実装する型である場合、MATLAB によって以下が表示されます。

  • 実際の Python 型

  • 基となるデータ

  • 対応する MATLAB 変換関数。この関数を使用して、Python オブジェクトを MATLAB 配列に完全に変換します。

たとえば、Python 関数は次の p 配列を返します。

p = 

  Python ndarray:

     8     1     6
     3     5     7
     4     9     2

    Use details function to view the properties of the Python object.

    Use double function to convert to a MATLAB array.

推奨された変換関数を使用して、p を MATLAB 行列 P に変換します。

P = double(p)
P = 3×3    
     8     1     6
     3     5     7
     4     9     2

p の Python プロパティに関する特定の情報を取得します。

details(p)
    py.numpy.ndarray handle with properties:

           T: [1×1 py.numpy.ndarray]
        base: [1×1 py.NoneType]
      ctypes: [1×1 py.numpy.core._internal._ctypes]
        data: [1×3 py.memoryview]
       dtype: [1×1 py.numpy.dtype[float64]]
       flags: [1×1 py.numpy.flagsobj]
        flat: [1×1 py.numpy.flatiter]
        imag: [1×1 py.numpy.ndarray]
    itemsize: [1×1 py.int]
      nbytes: [1×1 py.int]
        ndim: [1×1 py.int]
        real: [1×1 py.numpy.ndarray]
       shape: [1×2 py.tuple]
        size: [1×1 py.int]
     strides: [1×2 py.tuple]

  Methods, Events, Superclasses

Python モジュールが __doc__ 属性でコンテンツを提供する場合、MATLAB はその情報にリンクします。

参考

トピック