Use Matlab code in Python after creating standalone package
4 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Deepak Meena
on 11 Sep 2020
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 versions 2.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.py as :
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
See Also
Categories
Find more on Call MATLAB from Python in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!