如何从mathematica调用matlab函数?

发布于 2024-10-08 16:57:04 字数 191 浏览 5 评论 0原文

我想从mathematica 调用一个matlab 函数。如何最好地做到这一点?

我在 Wolfram 网站上找到了一篇 古老的帖子 描述了一种方法,是这样的吗还有连接两者的方法吗?

I would like to call a matlab function from mathematica. How best to do that?

I have found an ancient post on Wolfram site describing a way to do this, is this still the way to connect the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走过海棠暮 2024-10-15 16:57:04

您至少可以在Windows下尝试NETLink

In[1]:= Needs["NETLink`"]
matlab = CreateCOMObject["matlab.application"]

Out[2]= «NETObject[COMInterface[MLApp.DIMLApp]]»

然后您可以调用Matlab函数:

In[4]:= matlab@Execute["version"]

Out[4]= "
ans =

7.9.0.529 (R2009b)

"

In[5]:= matlab@Execute["a=2"]

matlab@Execute["a*2"]

Out[5]= "
a =

     2

"

Out[6]= "
ans =

     4

"

HTH

You can try NETLink for this at least under Windows:

In[1]:= Needs["NETLink`"]
matlab = CreateCOMObject["matlab.application"]

Out[2]= «NETObject[COMInterface[MLApp.DIMLApp]]»

And then you can invoke Matlab functions:

In[4]:= matlab@Execute["version"]

Out[4]= "
ans =

7.9.0.529 (R2009b)

"

In[5]:= matlab@Execute["a=2"]

matlab@Execute["a*2"]

Out[5]= "
a =

     2

"

Out[6]= "
ans =

     4

"

HTH

明天过后 2024-10-15 16:57:04

您可以使用mEngine。预编译的 Windows MathLink 可执行文件可与 Mathematica 8 配合使用。在 Windows 上,您可能需要添加 MATLAB到系统路径

与 NETLink 方法相比,这种方法的优点是在 Mathematica 和 MATLAB 之间传输变量就像 mGet["x"]mPut["x"] 一样简单。虽然这对于 NETLink 来说也是可能的,但 mEngine 的优点是您不需要自己实现它(如果像我一样您对 COM 或 .NET 一无所知,那么这很好)

You can use mEngine. The precompiled Windows MathLink executable works with Mathematica 8. On Windows you may need to add MATLAB to the system path.

The advantage of this compared to the NETLink method is that transferring variables between Mathematica and MATLAB will be as easy as mGet["x"] or mPut["x"]. Although this might be possible with NETLink too, the advantage of mEngine is that you don't need to implement it yourself (which is great if like me you don't know anything about COM or .NET)

柳若烟 2024-10-15 16:57:04

我认为这通常是一个困难的问题,但可以通过针对特定情况进行一些编程来轻松解决。我将用 C# 进行演示。

我会像这样建立一系列调用。

总而言之,我可以用不到 50 行 C# 代码来解决特定问题。换句话说,没有太多的工作。可能的问题是数据转换,但如果您想来回发送数据数组,MathLink 提供了很多开箱即用的功能。同样,Mathematica 可以通过 Java 连接到 MATLAB,尽管我自己还没有这样做。

也许最简单的连接可以通过 Python 进行。 Mathematica 提供了一个可安装的 MathLink python 库,位于 Mathematica\8.0\SystemFiles\Links\NETLink,Matlab 有一个名为 PyMat 的插件库,可以下载 这里,但是这个包已经维护了很长时间并且只支持最古老的Matlabs。

或者,您可以完全放弃 Matlab,转而使用 SAGE 和/或 numpy。

I would imagine that this is a difficult problem in general, but can be easily solved with a little programming for a particular case. I'll demonstrate with C#.

I would build a string of calls, like so.

  • Mathematica calls a C# program, through MathLink. This is near trivial to setup, and Mathematica has a sample project in Mathematica\8.0\SystemFiles\Links\NETLink directory.
  • C# program calls Matlab. There are several ways to make this call, and this handy link describes how to do it and offers sample code.
  • C# program returns Matlab results.

All in all I could do this in less than 50 lines of C# code, for a specific problem. Not too much work, in other words. Possible problems are data conversion, but if you want to send back and forth arrays of data, MathLink offers a lot out of the box. Similarly Mathematica can be linked to MATLAB through Java, though I haven't done that myself.

Perhaps the easiest connection could be made through Python. Mathematica offers an installable MathLink python library, located at Mathematica\8.0\SystemFiles\Links\NETLink, and Matlab has an addon library called PyMat, which can be downloaded here, but this package hasn't been maintained for a long time and supports only the most ancient of Matlabs.

Alternatively you can forgo Matlab altogether in favor of SAGE and/or numpy.

水水月牙 2024-10-15 16:57:04

现在有一个新的包用于此 --- MATLink。这是我所知道的最完整的此类包。 (免责声明:我是 MATLink 的开发人员之一。)

MATLink 让您...

  • 从 Mathematica 中无缝调用 MATLAB 函数

  • 在两个系统之间传输数据

    p>

支持大多数 MATLAB 数据类型,包括稀疏数组、结构体和单元格。

此处提供了更完整的说明。有关详细示例,请参阅网站

There is now a new package for this --- MATLink. It is the most complete such package I am aware of. (Disclaimer: I'm one of the developers of MATLink.)

MATLink lets you ...

  • seamlessly call MATLAB functions form Mathematica

  • transfer data between the two systems

Most MATLAB data types are supported, including sparse arrays, structs and cells.

A more complete description is available here. For detailed examples, see the website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文