MATLABでpythonのクラスの呼び出し

4 views (last 30 days)
tomoki takeuchi
tomoki takeuchi on 28 Jun 2019
Answered: Kojiro Saito on 29 Jun 2019
pythonで作成したコードのクラス内の情報を引継ぎ、中のメソッドを行いたいのですが、方法が分かりません。
class Hello():
def hello(self)
print "HelloWorld!"
このように作成し、helloのメソッドを仕様したい場合、MATLAB上にはどのように打ち込めばよいのでしょうか。
またpython上の問題があるのでしょうか。

Accepted Answer

Kojiro Saito
Kojiro Saito on 29 Jun 2019
以下のようなPythonファイルをMATLABのカレントディレクトリに置いておき、
helloClass.py
class Hello:
def hello(self):
print "HelloWorld!"
# print('HelloWorld!') # Python 3.x
MATLABから以下のように呼ぶことで使用できます。
myHelloClass = py.importlib.import_module('helloClass');
myHello = myHelloClass.Hello();
myHello.hello()
MATLABからPythonを呼び出す際の制約はこちらのドキュメントに記載があるので参考になると思います。

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!