在Python脚本中调用MATLAB .m文件和函数 [英] Calling MATLAB .m-files and functions in Python script

查看:1062
本文介绍了在Python脚本中调用MATLAB .m文件和函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于python脚本编写的平台,我想在其中调用matlab函数.我发现有几个线程可以解决该问题,其中两个线程

I have a platform for python scripting, and I would like to call matlab functions inside. I have found several threads tackling the issue, among them those two

如何与Python中的MATLAB交互?

从Python运行m文件

但是,线程不是最新的,还是不是很详细.

However, threads are either not recent, or not very detailed.

  1. mlabwrap可靠吗?
  2. 作为在Python脚本中调用matlab函数/.m文件的解决方案,您会提倡什么?
  3. 使用python中的win32com来调用matlab会话->这是一个好主意吗?您可以指出更多有关此主题的文档或示例吗?

看起来到sourceForge的链接不是最新的,最新更新为2010年,

looks like link to sourceForge is not up to date, last update 2010,

http://sourceforge.net/projects/mlabwrap/

  1. 您能提示一些最新版本吗?

谢谢

推荐答案

我仍然建议使用mlabwrap作为解决方案. 我在Linux和Windows上定期(每周?)使用mlabwrap,跨多个不同版本的Python和多个不同版本的Matlab.要回答您的特定问题:

I would still recommend mlabwrap as the solution for this. I use mlabwrap on a regular (weekly?) basis, on both Linux and Windows, across several different versions of Python, and several different versions of Matlab. To answer your specific questions:

  1. mlabwrap将在平台,Python和Matlab版本之间可靠地执行.但是,它确实有局限性,当超过这些局限性时,它将可靠地失败.通常,可以解决这些问题.
  2. 此处中查看我的答案有关通过mlabwrap调用Matlab函数和Matlab脚本的更多信息.该答案还描述了如何解决mlabwrap的主要限制之一,即并非所有的Matlab对象都可以直接转换为Python对象.
  3. 我对使用win32com调用Matlab一无所知.
  1. mlabwrap will reliably perform across platforms, Python, and Matlab versions. It does however have limitations, and it will reliably fail when pushed past those limitations. Usually, these can be worked around.
  2. See my answer here for more information on calling Matlab functions vs. Matlab scripts through mlabwrap. This answer also describes how to workaround one of the primary limitations of mlabwrap, which is that not all Matlab objects can be directly converted into Python objects.
  3. I don't know anything about calling Matlab using win32com.

我已经使用了mlabwrap,我将其称为"Python-primary"风格,其中大多数Python编程都使用Matlab作为特定数学函数的库,而scipy/numpy和in是"Matlab原始"样式,大部分编程都是在Matlab中进行的,最终结果被导入到Python中以便在某些外部过程中使用.

I have used mlabwrap in what I'll term 'Python-primary' style, where the majority of the programming in Python, using Matlab as a library for specific mathematical functions that aren't available in scipy/numpy, and in a 'Matlab-primary' style, the majority of the programming is in Matlab, and the final results are importedinto Python for use in some external process.

对于Python-primary,要记住的是,并非所有的Matlab函数都将返回Python可读的数据. mlabwrap将从这些函数返回MLabObjectProxy对象.当您使用Matlab函数创建对象并传递到其他Matlab函数以实际处理数据时,通常会发生这些情况.例如,您可以使用数字信号处理工具箱创建一个Welch频谱对象,然后将其用于获取数据的功率谱.从理论上讲,您可以将这些MLabObjectProxies传递到需要它们的Matlab函数中.以我的经验,您来回传递的次数越多,找到mlabwrap中的错误的可能性就越大.您可以做的是编写一个简单的Matlab包装函数来获取对象,处理数据,然后将适当的输出作为数组返回.

For Python-primary, the thing to keep in mind is that not all Matlab functions will return Python-readable data. mlabwrap will return a MLabObjectProxy object from these functions. These commonly occur when you use Matlab functions to create objects that are passed into other Matlab functions to actually process the data. For example, you can use the digital signal processing toolbox to create a Welch spectrum object which you can then use to get the power spectrum of your data. Theoretically, you can pass these MLabObjectProxies into Matlab functions that require them. In my experience the more you pass these back and forth, the more likely you are to find a bug in mlabwrap. What you can do instead is write a simple Matlab wrapper function obtains the object, processes the data, and then returns appropriate output as an array.

您还可以通过使用mlabwrap中的低级命令来解决MLabObjectProxies的问题.例如,如果我有一个matlab_struct,它是具有字段matlab_struct.label的结构数组,而我只想要Python端的标签,则可以执行以下操作:

You can also get around problems with the MLabObjectProxies by using the low-level commands in mlabwrap. For example, if I have a matlab_struct that is a struct array with field matlab_struct.label, and I only want the labels on the Python side, I can do the following:

# place matlab_struct into the Matlab workspace 
mlab._set('matlab_struct', matlab_struct) 
# convert the labels into a cell array
matlab_struct_labels = mlab.eval('{matlab_struct.labels}')

可用的主要低层命令是mlab._set('variable_name', variable)mlab.eval('command string')mlab.get('variable_name').

The main low-level commands available are mlab._set('variable_name', variable), mlab.eval('command string'), and mlab.get('variable_name').

如果我在Matlab中进行大量的处理工作,例如在其他地方无法使用的工具箱或插件中,我将编写所谓的"Matlab主代码",在这种情况下,我会尽量避免通过mlabwrap来回传递数据,而不是通过调用.m脚本,将结果输出保存到数据文件并将其导入到我的Python代码中来在Matlab工作区中操作变量.

If I'm doing a lot of heavy-duty processing in Matlab, say in a toolbox or plugin that isn't available elsewhere, I'll write what I call 'Matlab-primary' code, where I try to avoid passing data back and forth through mlabwrap, instead manipulating variables in the Matlab workspace by calling .m scripts, saving the resulting output to a data file, and importing that into my Python code.

祝你好运!

这篇关于在Python脚本中调用MATLAB .m文件和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆