python 调用matlab

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. The start_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

转载于:https://www.cnblogs.com/zhangli-ncu/p/8194407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值