如何从Python与MATLAB交互?

时间:2022-03-11 07:11:41

A friend asked me about creating a small web interface that accepts some inputs, sends them to MATLAB for number crunching and outputs the results. I'm a Python/Django developer by trade, so I can handle the web interface, but I am clueless when it comes to MATLAB. Specifically:

一位朋友问我创建一个接受一些输入的小型Web界面,将它们发送到MATLAB进行数字运算并输出结果。我是一名Python / Django开发人员,因此我可以处理Web界面,但是当谈到MATLAB时我很无能为力。特别:

  • I'd really like to avoid hosting this on a Windows server. Any issues getting MATLAB running in Linux with scripts created on Windows?
  • 我真的想避免在Windows服务器上托管这个。使用在Windows上创建的脚本在Linux中运行MATLAB的任何问题?

  • Should I be looking into shelling out commands or compiling it to C and using ctypes to interact with it?
  • 我应该考虑炮轰命令或将其编译为C并使用ctypes与它进行交互吗?

  • If compiling is the way to go, is there anything I should know about getting it compiled and working in Python? (It's been a long time since I've compiled or worked with C)
  • 如果编译是要走的路,那么我应该知道如何编译和使用Python吗? (自从我编译或使用C以来已经很长时间了)

Any suggestions, tips, or tricks on how to pull this off?

关于如何解决此问题的任何建议,提示或技巧?

5 个解决方案

#1


14  

There is a python-matlab bridge which is unique in the sense that Matlab runs in the background as a server so you don't have the startup cost each time you call a Matlab function.

有一个python-matlab桥,它在Matlab作为服务器在后台运行的意义上是独一无二的,因此每次调用Matlab函数时都没有启动成本。

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

#2


12  

Take a look at mlabwrap which allows you to call Matlab via a python API

看一下mlabwrap,它允许你通过python API调用Matlab

#3


6  

As an alternative, since Matlab R2014b, a Python library is provided to call Matlab from Python using the MATLAB Engine API for Python.

作为替代方案,自Matlab R2014b以来,提供了一个Python库,使用Python的MATLAB Engine API从Python调用Matlab。

#4


1  

Regarding OS compatibility, if you use the matlab version for Linux, the scripts written in windows should work without any changes. If possible, you may also consider the possibility of doing everything with python. Scipy/numpy with Matplotlib provide a complete Matlab replacement.

关于操作系统兼容性,如果您使用适用于Linux的matlab版本,则在Windows中编写的脚本应该无需任何更改即可运行。如果可能的话,你也可以考虑使用python做一切的可能性。 Matplotlib的Scipy / numpy提供了完整的Matlab替代品。

#5


-1  

Perhaps you'll find useful information here

也许你会在这里找到有用的信息

PyMat - An interface between Python and MATLAB

PyMat - Python和MATLAB之间的接口

#1


14  

There is a python-matlab bridge which is unique in the sense that Matlab runs in the background as a server so you don't have the startup cost each time you call a Matlab function.

有一个python-matlab桥,它在Matlab作为服务器在后台运行的意义上是独一无二的,因此每次调用Matlab函数时都没有启动成本。

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

#2


12  

Take a look at mlabwrap which allows you to call Matlab via a python API

看一下mlabwrap,它允许你通过python API调用Matlab

#3


6  

As an alternative, since Matlab R2014b, a Python library is provided to call Matlab from Python using the MATLAB Engine API for Python.

作为替代方案,自Matlab R2014b以来,提供了一个Python库,使用Python的MATLAB Engine API从Python调用Matlab。

#4


1  

Regarding OS compatibility, if you use the matlab version for Linux, the scripts written in windows should work without any changes. If possible, you may also consider the possibility of doing everything with python. Scipy/numpy with Matplotlib provide a complete Matlab replacement.

关于操作系统兼容性,如果您使用适用于Linux的matlab版本,则在Windows中编写的脚本应该无需任何更改即可运行。如果可能的话,你也可以考虑使用python做一切的可能性。 Matplotlib的Scipy / numpy提供了完整的Matlab替代品。

#5


-1  

Perhaps you'll find useful information here

也许你会在这里找到有用的信息

PyMat - An interface between Python and MATLAB

PyMat - Python和MATLAB之间的接口