简体   繁体   English

从 python 调用 MATLAB 函数

[英]Calling MATLAB functions from python

Is it possible to run MATLAB functions from within Python?是否可以从 Python 中运行 MATLAB 函数? I search the internet, I could only find PyMat.我在互联网上搜索,我只能找到 PyMat。 The bad thing is the compiled version only supports Python2.2 and I am using 2.6.不好的是编译版本只支持Python2.2,我用的是2.6。 So I tried to download the source code, so I can compile it for myself.所以我试着下载了源代码,这样我就可以自己编译了。 But I cannot compile it, VC++ express seems not to have the necessary functionalities to compile it.但是我无法编译它,VC++ express 似乎没有编译它所需的功能。 Does anyone have the compile version for PC?谁有PC版的编译版? or any substitutes for PyMat?或任何 PyMat 的替代品? Thanks谢谢

I know this is an old question and has been answered.我知道这是一个老问题并且已经得到解答。 But I was looking for the same thing (for the Mac) and found that there are quite a few options with different methods of interacting with matlab and different levels of maturity.但我一直在寻找相同的东西(对于 Mac),并发现有很多选项可以使用不同的方法与 matlab 进行交互,并且具有不同的成熟度。 Here's what I found:这是我发现的:

pymat皮垫

A low level interface to Matlab using the matlab engine ( libeng ) for communication (basically a library that comes with matlab).使用 matlab 引擎 ( libeng ) 进行通信的 Matlab 低级接口(基本上是 matlab 附带的库)。 The module has to be compiled and linked with libeng.该模块必须编译并与 libeng 链接。

http://pymat.sourceforge.net http://pymat.sourceforge.net
Last updated: 2003最后更新:2003

pymat2 pymat2

A somewhat short lived continuation of the pymat development. pymat 开发的短暂延续。 Seems to work on windows (including 64bit), linux and mac (with some changes).似乎适用于 windows(包括 64 位)、linux 和 mac(有一些变化)。

https://code.google.com/p/pymat2/ https://code.google.com/p/pymat2/
Last updated: 2012最后更新:2012

mlabwrap包装纸

A high level interface that also comes as a module which needs compilation and linking against libeng .一个高级接口,也作为一个模块出现,需要编译和链接libeng It exposes Matlab functions to python so you can do fun stuff like它将 Matlab 函数暴露给 python,所以你可以做一些有趣的事情,比如

mlab.plot(x, y, 'o')

http://mlabwrap.sourceforge.net http://mlabwrap.sourceforge.net
Last updated: 2009最后更新:2009

mlab实验室

A repackaging effort of mlabwrap. mlabwrap 的重新包装工作。 Basically it replaces the c++ code that links against 'libeng' in mlabwrap with a python module ( matlabpipe ) that communicates with matlab through a pipe.基本上,它取代了C ++代码,对在与一个python模块(mlabwrap“libeng”链接matlabpipe ),其与通过管道MATLAB连通。 The main advantage of this is that it doesn't need compilation of any kind.这样做的主要优点是它不需要任何类型的编译。

Unfortunately the package currently has a couple of bugs and doesn't seem to work on the mac at all.不幸的是,该软件包目前有几个错误,似乎根本无法在 mac 上运行。 I reported a few of them but gave up eventually.我报告了其中的一些,但最终放弃了。 Also, be prepared for lots of trickery and a bunch of pretty ugly hacks if you have to go into the source code ;-) If this becomes more mature it could be one of the best options.此外,如果您必须进入源代码,请准备好接受大量诡计和一堆非常丑陋的 hack ;-) 如果这变得更加成熟,它可能是最好的选择之一。

https://github.com/ewiger/mlab https://github.com/ewiger/mlab
last update: 2013最后更新:2013

pymatlab pymatlab

A newer package (2010) that also interacts with Matlab through libeng .一个更新的包(2010)也通过libeng与 Matlab libeng Unlike the other packages this one loads the engine library through ctypes thus no compilation required.与其他包不同,这个包通过ctypes加载引擎库,因此不需要编译。 Its not without flaws but still being maintained and the (64bit Mac specific) issues I found should be easy enough to fix.它并非没有缺陷,但仍在维护中,我发现的(特定于 64 位 Mac 的)问题应该很容易解决。
( edit 2014-05-20 : it seems those issues have already been fixed in the source so things should be fine with 0.2.4) 编辑 2014-05-20 :似乎这些问题已经在源代码中得到修复,所以 0.2.4 应该没问题)

http://pymatlab.sourceforge.net http://pymatlab.sourceforge.net
last update: 2014最后更新:2014

python-matlab-bridge python-matlab-bridge

Also a newer package that is still actively maintained.也是一个仍在积极维护的较新软件包。 Communicates with Matlab through some sort of socket.通过某种套接字与 Matlab 通信。 Unfortunately the exposed functions are a bit limited.不幸的是,公开的功能有点有限。 I couldn't figure out how to invoke a function that takes structs as parameters.我不知道如何调用一个将结构作为参数的函数。 Requires zmq, pyzmq and IPython which are easy enough to install.需要很容易安装的 zmq、pyzmq 和 IPython。

http://arokem.github.io/python-matlab-bridge http://arokem.github.io/python-matlab-bridge
last update: 2014最后更新:2014

Another option is Mlabwrap :另一种选择是Mlabwrap

Mlabwrap is a high-level python to Matlab® bridge that lets Matlab look like a normal python library. Mlabwrap 是一个高级 python 到 Matlab® 桥,它让 Matlab 看起来像一个普通的 python 库。

It works well with numpy arrays.它适用于 numpy 数组。 An example from the home page:来自主页的示例:

>>> from mlabwrap import mlab; from numpy import *
>>> xx = arange(-2*pi, 2*pi, 0.2)
>>> mlab.surf(subtract.outer(sin(xx),cos(xx)))

PyMat looks like it's been abandoned. PyMat 看起来已经被废弃了。

I'm assuming you are on windows so you could always do the simplest approach and use Matlab's COM interface:我假设你在 Windows 上,所以你总是可以做最简单的方法并使用 Matlab 的 COM 接口:

>>> import win32com.client
>>> h = win32com.client.Dispatch('matlab.application')
>>> h.Execute ("plot([0 18], [7 23])")
>>> h.Execute ("1+1")
u'\nans =\n\n     2\n\n'

More info here更多信息在这里

There is a python-matlab bridge which is unique in the sense that Matlab runs in the background so you don't have the startup cost each time you call a Matlab function.有一个 python-matlab 桥,它是独一无二的,因为 Matlab 在后台运行,因此每次调用 Matlab 函数时都没有启动成本。 https://github.com/jaderberg/python-matlab-bridge https://github.com/jaderberg/python-matlab-bridge

it's as easy as downloading and the following code:它就像下载和以下代码一样简单:

from pymatbridge import Matlab
mlab = Matlab(matlab='/Applications/MATLAB_R2011a.app/bin/matlab')
mlab.start()
res = mlab.run('path/to/yourfunc.m', {'arg1': 3, 'arg2': 5})
print res['result']

where the contents of yourfunc.m would be something like this: yourfunc.m 的内容是这样的:

%% MATLAB
function lol = yourfunc(args)
    arg1 = args.arg1;
    arg2 = args.arg2;
    lol = arg1 + arg2;
end

I would like to add one more option to the excellent summary by Lukas:我想在 Lukas 的优秀总结中再添加一个选项:

matlab_wrapper matlab_wrapper

The advantage of matlab_wrapper is that it is pure Python library and you will not need to compile anything. matlab_wrapper 的优点是它是纯 Python 库,您不需要编译任何东西。 Works in GNU/Linux, Windows and OSX.适用于 GNU/Linux、Windows 和 OSX。

https://github.com/mrkrd/matlab_wrapper https://github.com/mrkrd/matlab_wrapper

Disclaimer: I'm the author of matlab_wrapper免责声明:我是 matlab_wrapper 的作者

You can use the official matlab engine by installing Matlab, then building python engine from its extern files.您可以通过安装 Matlab 来使用官方的 matlab 引擎,然后从其外部文件构建 python 引擎。 You can check the guide website below: ---Thanks for the advice in the first comment of this answer --- the essential step in brief are (On Windows platform, other can checked in the url below): 1. download and then install matlab, the version must be R2014 or later.您可以查看下面的指南网站: ---感谢您在此答案的第一条评论中的建议 --- 简要的必要步骤是(在 Windows 平台上,其他可以在下面的 url 中查看): 1. 下载然后安装matlab,版本必须是R2014或更高版本。 2. open a PowerShell window under admin, then: 2.在admin下打开一个PowerShell窗口,然后:

cd "matlabroot\extern\engines\python"

3. use command-line below to install: 3.使用下面的命令行安装:

python setup.py install

The admin is essential, or you'll fail to build it.管理员是必不可少的,否则您将无法构建它。

For more information, you can click the official start sheet below: http://cn.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html更多信息可以点击下面的官方启动表: http : //cn.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html

newer versions of matlab seem to provide a module that allows you to call matlab functions from within python.较新版本的 matlab 似乎提供了一个模块,允许您从 python 中调用 matlab 函数。 see here and here .看到这里这里

2 more options for you to consider:还有 2 个选项供您考虑:

  1. Follow the official MATLAB docs: Create a Python Application with MATLAB Code .遵循官方 MATLAB 文档: 使用 MATLAB 代码创建 Python 应用程序 This will create a Python library that includes MATLAB runtime which you can call from within your Python code.这将创建一个包含 MATLAB 运行时的 Python 库,您可以从 Python 代码中调用它。

  2. Run your MATLAB code in GNU Octave then call it from Python using Oct2PyGNU Octave 中运行您的 MATLAB 代码,然后使用Oct2Py从 Python 中调用它

This is the solution from Mathworks.这是 Mathworks 的解决方案。

In your current folder, create a MATLAB script in a file named triarea.m.在当前文件夹中,在名为 triarea.m 的文件中创建一个 MATLAB 脚本。

function a = triarea(b,h)
a = 0.5*(b.* h);

Meanwhile, you run the python code as follows,同时,您按如下方式运行 python 代码,

import matlab.engine
eng = matlab.engine.start_matlab()
eng.addpath('your/code/folders/')
ret = eng.triarea(1.0,5.0)
print(ret)
>>> 2.5

Matlab already provides the python module of the engine, to install that you can do the following, Matlab 已经提供了引擎的python 模块,要安装,您可以执行以下操作,

cd matlab_root_folder/extern/engines/python
python setup.py install

You are all done!你都完成了! Tips: you need to be careful about the data type, the engine is not friendly with numpy. Tips:需要注意数据类型,引擎对numpy不友好。 You need to convert the data first.您需要先转换数据。

mat_array = matlab.double(list(my_numpy_array))
eng.my_matlabe_function(mat_array )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM