我正在尝试将Python 2.6嵌入到MATLAB(7.12)中。我想嵌入一个用C编写的mex文件。这对于使用标量的小型简单示例很有用。但是,如果导入Numpy(1.6.1),MATLAB崩溃了。无论如何我说,因为我尝试了很多方法来加载numpy库,包括
在python模块(.py)中:
from numpy import *
在mex文件中使用PyRun_SimpleString
:
PyRun_SimpleString(“from numpy import *”);
使用Py_oBject_CallObject
调用numpy函数:
pOut = PyObject_CallObject(pFunc, pArgs);
最初,我认为这可能是将Numpy嵌入C中的问题。但是,当嵌入在使用Visual Studio 2005 C编译器的/ MD(多线程)开关从命令行编译的简单C文件中时,Numpy工作正常。接下来,我想我只需要在MATLAB中更改make文件以包含/ MD开关。没有这样的运气,mexopts.bat与/ MD开关编译。我还手动注释了Numpy init模块中的行,以找出崩溃的MATLAB。似乎加载任何扩展名为pyd的文件会导致MATLAB崩溃。在NumPy中加载的第一个这样的文件是multiarray.pyd。 MATLAB文档描述了如何使用Visual Studio调试mex文件,并在下面放置了错误消息。在这一点上,我知道问题是pyd的内存问题和MATLAB的一些冲突。有趣的是,我可以在MATLAB中使用系统命令来启动使用numpy的python中的进程,并且不会生成错误。我将在MATLAB的错误消息下面粘贴,然后在可视化工作室中粘贴MATLAB崩溃的过程中的DEBUG输出。但是,我并没有粘贴整个事情,因为第一次机会的例外列表很长。有没有解决这个集成问题的建议?
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 Studios DEBUGGER的输出
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
答案 0 :(得分:4)
尝试从Python方面解决问题:Python是一种很棒的粘合语言,我建议你让Python运行你的Matlab和C程序。 Python有:
因此,对于几乎任何现有的Matlab模块,该组合都是一个很好的选择。
答案 1 :(得分:0)
使用matlab 2014b,可以直接在m代码中添加call python functions。