1.安装matlab engine api
Build in Nondefault Folder, Install in Default Folder
If you do not have write permission to build the engine in the MATLAB® folder, use a nondefault folder, builddir
.
cd "matlabroot\extern\engines\python" python setup.py build --build-base="builddir" install
2.调用引擎
-
Start Python® at the operating system prompt.
-
Import the
matlab.engine
package into your Python session. -
Start a new MATLAB® process by calling
start_matlab
. Thestart_matlab
function returns a Python object,eng
, which allows you to pass data and call functions executed by MATLAB.
import matlab.engine eng = matlab.engine.start_matlab()
3.调用m文件
In your current folder, create a MATLAB script in a file named triarea.m
.
b = 5; h = 3; a = 0.5*(b.* h)
After you save the file, start Python and call the script.
import matlab.engine eng = matlab.engine.start_matlab() eng.triarea(nargout=0)
a = 7.5000
Specify nargout=0
. Although the script prints output, it returns no output arguments to Python.
这种方法没有返回值。如果要返回值,将m文件改写为函数形式
function a = triarea(b,h)
a = 0.5*(b.* h);
参考官方文档
http://cn.mathworks.com/help/matlab/matlab-engine-for-python.html