在MATLAB中嵌入Python

2024-09-27 19:28:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将Python2.6嵌入MATLAB(7.12)中。我想嵌入一个用C语言编写的mex文件,这对于使用标量的简单小例子来说很好。但是,如果Numpy(1.6.1)在任何情况下被导入,MATLAB就会崩溃。我之所以这么说,是因为我尝试了很多方法来加载numpy库,包括

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

    from numpy import *
    
  2. 在mex文件中使用PyRun_SimpleString

    PyRun_SimpleString(“from numpy import *”);
    
  3. 使用Py_oBject_CallObject调用numpy函数:

    ^{3美元

最初,我认为这可能是在C中嵌入Numpy的一个问题。但是,当Numpy嵌入到简单的C文件中时,Numpy可以正常工作,这些文件是使用visualstudios2005c编译器通过/MD(多线程)开关从命令行编译的。接下来,我想我只需要在MATLAB中更改make文件,以包含/MD开关。没有这样的运气,墨索普斯.bat使用/MD开关编译。我还手动注释了Numpy init模块中的行,以找到导致MATLAB崩溃的原因。似乎加载任何扩展名为pyd的文件都会导致MATLAB崩溃。在NumPy中加载的第一个这样的文件是多数组.pyd. MATLAB文档描述了如何使用visualstudios调试mex文件,并在下面给出了错误消息。在这一点上,我知道问题是与pyd的内存问题和一些与MATLAB的冲突。有趣的是,我可以使用MATLAB中的系统命令启动python中使用numpy的进程,并且不会生成错误。我将在下面粘贴来自MATLAB的错误消息,然后在visualstudios中粘贴导致MATLAB崩溃的进程的调试输出。但是,我并没有粘贴全部内容,因为第一次机会例外的列表非常长。有没有解决这个整合问题的建议?在

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

Tags: 文件infromnumpyexceptionlocationexeat
2条回答

试着从Python方面来解决这个问题:Python是一种很棒的粘合语言,我建议您让Python运行Matlab和C程序。Python有:

  1. Numpy公司
  2. 幽门
  3. Matplotlib库
  4. 伊普顿

因此,对于几乎任何现有的Matlab模块来说,这种组合都是一个很好的替代方案。在

在matlab2014b中,增加了直接在m代码中call python functions的可能性。在

相关问题 更多 >

    热门问题