OCT2Py cho phép bạn gọi một cách liền mạch các chức năng M-Files và Octave từ Python. Nó quản lý phiên OCTAVE cho bạn, chia sẻ dữ liệu đằng sau hậu trường bằng các tệp MAT. Việc sử dụng đơn giản như:
>>> import oct2py
>>> oc = oct2py.Oct2Py[]
>>> x = oc.zeros[3,3]
>>> print[x, x.dtype]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]] float64
Để chạy chức năng .m, bạn cần thêm rõ ràng đường dẫn vào tệp .m bằng cách sử dụng:
>>> from oct2py import octave
>>> # to add a folder use:
>>> octave.addpath['/path/to/directory'] # doctest: +SKIP
>>> # to add folder with all subfolder in it use:
>>> octave.addpath[octave.genpath['/path/to/directory']] # doctest: +SKIP
>>> # to run the .m file :
>>> octave.run['fileName.m'] # doctest: +SKIP
Để lấy đầu ra của tệp .m Sau khi đặt đường dẫn, hãy sử dụng:
>>> import numpy as np
>>> from oct2py import octave
>>> x = np.array[[[1, 2], [3, 4]], dtype=float]
>>> #use nout='max_nout' to automatically choose max possible nout
>>> octave.addpath['./example'] # doctest: +SKIP
>>> out, oclass = octave.roundtrip[x,nout=2] # doctest: +SKIP
>>> import pprint # doctest: +SKIP
>>> pprint.pprint[[x, x.dtype, out, oclass, out.dtype]] # doctest: +SKIP
[array[[[1., 2.],
[3., 4.]]],
dtype['float64'],
array[[[1., 2.],
[3., 4.]]],
'double',
dtype['