在MATLAB中嵌入Python [英] Embedding Python in MATLAB

查看:103
本文介绍了在MATLAB中嵌入Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Python 2.6嵌入到MATLAB(7.12)中.我想嵌入一个用C编写的mex文件.这对于使用标量的简单示例来说效果很好.但是,无论如何如果导入了Numpy(1.6.1),MATLAB都会崩溃.我之所以说是因为我已经尝试了多种方式来加载numpy库,包括

I am trying to embed Python 2.6 into MATLAB (7.12). I wanted to embed with a mex file written in C. This worked fine for small simple examples using scalars. However, if Numpy (1.6.1) is imported in anyway MATLAB crashes. I say anyway because I have tried a number of ways to load the numpy libraries including

  1. 在python模块(.py)中:

  1. In the python module (.py):

from numpy import *

  • 在mex文件中带有PyRun_SimpleString:

    PyRun_SimpleString("from numpy import *");
    

  • 使用Py_oBject_CallObject调用numpy函数:

  • Calling numpy functions with Py_oBject_CallObject:

    pOut  = PyObject_CallObject(pFunc, pArgs); 
    

  • 最初,我认为这可能是将Numpy嵌入C中的问题.但是,将Numpy嵌入到通过Visual Studios 2005 C编译器从命令行通过/MD(多线程)开关编译的简单C文件中时,可以正常工作.接下来,我以为我将在MATLAB中更改make文件以包含/MD开关.没有这种运气,mexopts.bat使用/MD开关进行编译.我还手动注释了Numpy init模块中的行,以查找崩溃的MATLAB.似乎加载任何扩展名为pyd的文件都会使MATLAB崩溃.在NumPy中加载的此类文件中的第一个是multiarray.pyd. MATLAB文档描述了我如何使用Visual Studio调试mex文件并将错误消息放在下面.至此,我知道问题是pyd的内存问题以及与MATLAB的某些冲突.有趣的是,我可以在MATLAB中使用系统命令来启动python中使用numpy的进程,并且不会产生任何错误.我将粘贴来自MATLAB的错误消息,然后在Visual Studio中粘贴使MATLAB崩溃的进程的DEBUG输出.但是,我没有粘贴整个内容,因为初次例外的列表很长.有解决此集成问题的建议吗?

    Originally, I thought this may be a problem with embedding Numpy in C. However, Numpy works fine when embedded in simple C files that are compiled from the command line with /MD (multithread) switch with the Visual Studios 2005 C compiler. Next, I thought I will just change the make file in MATLAB to include the /MD switch. No such luck, mexopts.bat compiles with the /MD switch. I also manually commented out lines in the Numpy init module to find what was crashing MATLAB. It seems that loading any file with the extension pyd crashes MATLAB. The first of such files loaded in NumPy is multiarray.pyd. The MATLAB documentation describes how to debug mex files with visual studios which I did and placed the error message below. At this point I know the problem is a memory problem with the pyd’s and some conflict with MATLAB. Interestingly, I can use a system command in MATLAB to kick off a process in python that uses numpy and no error is generated. I will paste below the error message from MATLAB followed by the DEBUG output in visual studios of the processes that crash MATLAB. However, I am not pasting the whole thing because the list of first-chance exceptions is very long. Are there any suggestions for solving this integration problem?

    MATLAB error
    Matlab has encountered an internal problem and needs to close 
    MATLAB crash file:C:\Users\pml355\AppData\Local\Temp\matlab_crash_dump.3484-1:
    
    
    ------------------------------------------------------------------------
           Segmentation violation detected at Tue Oct 18 12:19:03 2011
    ------------------------------------------------------------------------
    
    Configuration:
      Crash Decoding  : Disabled
      Default Encoding: windows-1252
      MATLAB License  : 163857
      MATLAB Root     : C:\Program Files\MATLAB\R2011a
      MATLAB Version  : 7.12.0.635 (R2011a)
      Operating System: Microsoft Windows 7
      Processor ID    : x86 Family 6 Model 7 Stepping 10, GenuineIntel
      Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
      Window System   : Version 6.1 (Build 7600)
    
    Fault Count: 1
    
    Abnormal termination:
    Segmentation violation
    
    Register State (from fault):
      EAX = 00000001  EBX = 69c38c20
      ECX = 00000001  EDX = 24ae1da8
      ESP = 0088af0c  EBP = 0088af44
      ESI = 69c38c20  EDI = 24ae1da0
    
      EIP = 69b93d31  EFL = 00010202
    
       CS = 0000001b   DS = 00000023   SS = 00000023
       ES = 00000023   FS = 0000003b   GS = 00000000
    
    
    Stack Trace (from fault):
    [  0] 0x69b93d31 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00081201 ( ???+000000 )
    [  1] 0x69bfead4 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00518868 ( ???+000000 )
    [  2] 0x69c08039 C:/Python26/Lib/site-packages/numpy/core/multiarray.pyd+00557113 ( ???+000000 )
    [  3] 0x08692b09                           C:/Python26/python26.dll+00076553 ( PyEval_EvalFrameEx+007833 )
    [  4] 0x08690adf                           C:/Python26/python26.dll+00068319 ( PyEval_EvalCodeEx+002255 )
    
    
    
    This error was detected while a MEX-file was running. If the MEX-file
    is not an official MathWorks function, please examine its source code
    for errors. Please consult the External Interfaces Guide for information
    on debugging MEX-files.
    
    If this problem is reproducible, please submit a Service Request via:
        http://www.mathworks.com/support/contact_us/
    
    A technical support engineer might contact you with further information.
    
    Thank you for your help. 
    

    Visual Studio调试器的输出

    First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
    First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
    First-chance exception at 0x0c12c128 in MATLAB.exe: 0xC0000005: Access violation reading location 0x00000004.
    First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e210..
    First-chance exception at 0x751d9673 in MATLAB.exe: Microsoft C++ exception: jitCgFailedException at memory location 0x00c3e400..
    First-chance exception at 0x69b93d31 in MATLAB.exe: 0xC0000005: Access violation writing location 0x00000001.
    > throw_segv_longjmp_seh_filter()
    throw_segv_longjmp_seh_filter(): invoking THROW_SEGV_LONGJMP SEH filter
    > mnUnhandledWindowsExceptionFilter()
    MATLAB.exe has triggered a breakpoint
    

    推荐答案

    尝试从Python角度解决问题:Python是一种很棒的粘合语言,我建议您让Python运行Matlab和C程序. Python具有:

    Try to approach the problem from the Python side: Python is a great glue language, I would suggest you to have Python run your Matlab and C programs. Python has:

    1. 脾气暴躁
    2. PyLab
    3. Matplotlib
    4. IPython

    因此,这种组合是几乎所有现有Matlab模块的不错选择.

    Thus, the combination is a good alternative for almost any existing Matlab module.

    这篇关于在MATLAB中嵌入Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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