Use Matlab code in Python after creating standalone package

4 次查看(过去 30 天)
I am using MATLAB R_2020a and pycharm editor on MacOS, I want to use my MATLAB project in python code.
I followed the instruction to get the package, however, my MATLAB project has many folders and the generated folders after creating a package does not have any files inside the Sample folder as in MATLAB tutorial ( which includes the equivalent .py code).
My question is how to import in python the packge and how to start running the code and calling the functions inside the pycharm.
Any help is really appreciated.
here is a simple example of creating sumTwoNum MATLAB package for Python, and it has a sumTwoNum(a,b) function. please let me know how to call this function in python.

回答(1 个)

Deepak Meena
Deepak Meena 2020-9-11
Hi Maryam ,
The MATLAB Engine API for Python provides a package for Python to call MATLAB as a computational engine. The engine supports the reference implementation (CPython). MATLAB supports versions2.7, 3.6, and 3.7. So, make sure your python version is one of them.
Now let's assume the following MATLAB script sumTwoNum.m :
function m =sumTwoNum(a,b)
m = a + b;
end
Make sure that your MATLAB script is in MATLAB path.
Now you can call this function in python file test.pyas :
import matlab.engine
eng = matlab.engine.start_matlab()
p = eng.sumTwoNum(3,2);
print(p);
You can run the python script as follows
>python2 test.py
5
For more information,you can refer to this document
  1 个评论
MatlabUser
MatlabUser 2020-9-11
编辑:MatlabUser 2020-9-11
Thank you Deepak Meena for your answer,
Actually, I am trying to use it as a package, without the need of MATLAB installation as here (Documentation).
I read that there is two ways to use MATLAB in Python, one of them exactly as you answered but the second one is by making a package that you just import it. The problem that I am facing is that I couldn't fined the .py file that I need to import after creating the package.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by