Calling Python - C++ from Matlab

2 ビュー (過去 30 日間)
Simone
Simone 2014 年 11 月 7 日
コメント済み: Geoff Hayes 2014 年 11 月 7 日
Hi,
firstly I would like to say that I am a real beginner in Matlab. It's the first time I use it and I'm trying to learn. So I'm truly sorry if the question I'm going to make could seem silly. Yet I'm not a complete beginner in coding: I know C, C++ and I'm trying to learn Python.
My question is very simple: if I have a .py file, let's say "Hello World", can I execute it from Matlab? If it is possible, how can I do that? Or can I just call the python functions?
Because I was wandering if I can exploit the knowledge I already have integrating it with the matlab environment.
I'm running matlab R2014b amd64 on Linux.
Thanks,
Cheers!
Simone

回答 (2 件)

Geoff Hayes
Geoff Hayes 2014 年 11 月 7 日
Simone - yes, if you have a simple .py file then you can execute it in MATLAB. For example, if your helloWorld.py file contains the following code
import sys
if __name__ == '__main__':
sys.stdout.write('Hello World\n')
then you can execute this from MATLAB as
system('python helloWorld.py');
which would return
Hello World
But since you have R2014b, you can do a lot more with Python. See calling Python libraries and calling user-defined custom modules for more cool stuff that you can do.

Simone
Simone 2014 年 11 月 7 日
Thank you! One more question: is it right for C++ codes as well?
  1 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 11 月 7 日
I suppose you could use system for similar calls to a C++ executable, but to interface with existing C/C++ code you could create MEX files.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by