Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

Python への MATLAB データの引き渡し

Python® 関数を呼び出す際、MATLAB® は MATLAB データを、Python 言語に対してデータを最も適切に表す型に変換します。MATLAB における Python データの使用の詳細については、Python 関数から返されたデータの処理を参照してください。

スカラー値を Python に渡す

MATLAB 入力引数の型 —
スカラー値のみ

結果の Python py.

double (実数)
single (実数)

float

MATLAB での Python 数値変数の使用

double (複素数)
single (複素数)

complex

z = complex(1,2);
py.cmath.polar(z)
ans = 
  Python tuple with no properties.

    (2.23606797749979, 1.1071487177940904)

int8
uint8
int16
uint16
int32

int

 

uint32
int64
uint64

int

 

NaN

float("nan")

 

Inf

float("inf")

 

string スカラー
char ベクトル

str

MATLAB での Python str 変数の使用

string<missing>

None

py.list({string(missing),'Value'})
ans = 
  Python list with no properties.

    [None, 'Value']

logical

bool

 

struct

dict

MATLAB での Python dict 変数の使用
datetime

py.datetime.datetime

Python での MATLAB datetime 型の使用
duration

py.datetime.timedelta

Use MATLAB duration Types with Python

Python オブジェクト — py.type

type

 

Python 関数専用の関数ハンドル @py.module.function

module.function

Python 関数 map への Python 関数の受け渡し

ベクトルを Python に渡す

MATLAB 入力引数の型 —
1N 列のベクトル

結果の Python 型

double (実数)

array.array('d')

single (実数)

array.array('f')

int8 (実数)

array.array('b')

uint8 (実数)

array.array('B')

int16 (実数)

array.array('h')

uint16 (実数)

array.array('H')

int32 (実数)

array.array('i')

uint32 (実数)

array.array('I')

int64 (実数)

array.array('q')

uint64 (実数)

array.array('Q')

double (複素数)
single (複素数)

memoryview

logical

memoryview

char ベクトル

str

cell ベクトル

tuple

行列と多次元配列を Python に渡す

Python 言語は MATLAB 配列に格納されたデータなど、メモリ バッファーにアクセスするためのプロトコルを提供します。MATLAB は、この Python バッファー プロトコルを MATLAB 配列に対して実装するため、データをコピーしなくても、MATLAB と同じプロセスで実行されている Python コードから MATLAB 配列を直接読み取ることができます。

多くの Python 関数は、ネイティブな Python 型に変換せずに、Python から MATLAB 配列を直接使用します。一部の関数では、numpy.ndarray などの特定の型が必要になる場合や、配列内のデータが変更される場合があります。これらの関数は MATLAB 配列を受け入れて、データを必要な型にコピーする場合があります。他の関数では、必要な型を渡さないとエラーを表示する場合もあります。これらの関数にデータを渡すには、まず、MATLAB データから必要な Python 型を作成して、それを Python 関数に渡します。この例では、numpy.array 型のデータ型を必要とする Python 関数に渡すための p 配列を作成します。

p = py.numpy.array(magic(3))
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.

MATLAB スパース配列は、Python ではサポートされていません。Unsupported MATLAB Types を参照してください。

引数エラーのトラブルシューティング

Python 関数に numpy.ndarray など特定の Python 多次元配列型が必要である場合、MATLAB は、続行するためのヒントを含むメッセージを表示します。問題の原因が、行列または多次元配列を引数として渡すことである場合は、以下を実行します。

  1. Python 関数のドキュメントを確認し、想定される引数の型を調べる。

  2. MATLAB でその型の Python オブジェクトを作成し、それを Python 関数に渡す。

たとえば、次のコードがエラーを返すとします。

a = [1 2; 3 4];
py.pyfunc(a)

pyfunc のドキュメントで、想定される型が numpy.ndarray であると指定されている場合、次の変換を試します。

py.pyfunc(numpy.ndarray(a))

エラーが解決しない場合は、Python 例外の追加情報を確認して根本原因を特定します。

サポートされていない MATLAB

以下の MATLAB 型は、Python ではサポートされていません。

  • 多次元の char または cell 配列

  • スパース配列

  • struct 配列

  • categorical

  • table

  • containers.Map

  • MATLAB オブジェクト

  • meta.class (py.class)

関連する例

詳細