Use Matlab code in Python after creating standalone package
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Deepak Meena
am 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
1 Kommentar
Siehe auch
Kategorien
Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!