102 Comments

  1. Rann

    No matter what I do, I get: ImportError: /usr/lib/pymodules/python2.7/numpy/core/multiarray.so: undefined symbol: PyExc_SystemError

    What am I doing wrong? I’ve compiled against Python 2.7 using the command mex py.cpp -lpython2.7 -ldl

  2. Amro

    you might wanna wrap Linux-specific code inside a macro. Two lines would be affected: including “dlfcn.h” header, and the dlopen() call.

  3. Brad

    I compile with: mex py.cpp -lpython2.7 -ldl

    When I run:

    a=1; py_export(‘a’)

    I get:

    Traceback (most recent call last):
    File “/usr/lib/python2.7/dist-packages/numpy/__init__.py”, line 137, in
    import add_newdocs
    File “/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py”, line 9, in
    from numpy.lib import add_newdoc
    File “/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py”, line 13, in
    from polynomial import *
    File “/usr/lib/python2.7/dist-packages/numpy/lib/polynomial.py”, line 11, in
    import numpy.core.numeric as NX
    AttributeError: ‘module’ object has no attribute ‘core’
    Error using py
    numpy not accessible

    I have already done the following:
    1) Changed reference in py.cpp from libpython2.6.so to libpython2.7.so
    2) Added an environment variable in the terminal with export LD_PRELOAD=”libstdc++.6.so” (& then started MATLAB)
    3) Add the LDFLAGS=”$LDFLAGS -Xlinker -export-dynamic” after the other LDFLAGS in the archetecture section of my mexopts.sh

    If I try to compile with just “mex py.cpp -lpython2.7”, I get a dlopen failure:
    py.cpp:(.text+0x1d18): undefined reference to `dlopen’
    collect2: ld returned 1 exit status

      • Brad

        Yes, I can succesfully do an “import numpy” in the python console. Should I have a path on the LD_PRELOAD? Also if I just run “py” from the MATLAN console, I get the “numpy not accessible”. Any troubleshooting tips/helps are much appreciated…. I would love for this to work. Thanks.

          • Brad

            Yes, I changed the include also. I tried it on a second machine. My process:

            1) g++ is installed: whereis g++ (in terminal)
            2) Verified python development environment is installed
            3) In MATLAB: mex -setup and set compiler. This should create mexopts.sh in local user folder.
            4) Verify arch by looking in /usr/local/MATLAB/R2011b/sys/os
            5) Edit he mexopts.sh
            It should be located in: ~/.matlab/R2011b/mexopts.sh (sudo gedit)
            Scroll down to the case statement for the architecture
            Add as a new line after the LDFLAGS=”-pthread -shared -….. line: LDFLAGS=”$LDFLAGS -Xlinker -export-dynamic”
            6) Copy matpy zip and extract it into a new folder in ~/Documents/MATLAB/matpy. Add this folder to the path in MATLAB.
            7) open py.cpp. Change python2.6 references to python2.7. There are 2 of these, an include python.h and the dlopen open statement.
            8) Close MATLAB. In a terminal: LD_PRELOAD=”libstdc++.so.6″ /usr/local/MATLAB/R2011b//bin/matlab -desktop
            9) Change MATLAB’s working directory ~/Documents/MATLAB/matpy
            10) Run in MATLAB: mex py.cpp -lpython2.7 -ldl
            11) Run in MATLAB: py
            and I get:
            Error using py
            numpy not accessible

            At step 10 get a couple of warnings but it seems to compile:
            py.cpp: In function ‘mxArray* py2mat(PyObject*)’:
            py.cpp:229:64: warning: deprecated conversion from string constant to ‘char*’

            There is a typo btw in your LD_PRELOAD=”libstdc++.6.so above. I think it should be LD_PRELOAD=”libstdc++.so.6

          • admin

            Yes, of course it’s a typo. But otherwise your step-by-step description seems fine to me. At this point I think I would run MATLAB with strace to determine exactly what files are being loaded by the embedded Python interpreter and more precisely to get an idea of what numpy is trying to load and why it fails. I suspect it’s again some dynamic linking problem and numpy cannot load one of its modules from a shared library. I haven’t experienced it myself on any of the computers I’ve tested matpy on.

          • Shiquan Wang

            It seems something related to libgfortran
            refer to http://stackoverflow.com/questions/9628273/libgfortran-version-gfortran-1-4-not-found

            I did two things to make things right:
            1. Solve the Mex warning due to gcc version, problem remains after this;
            2. Solve the libgfortran thing according to: http://stackoverflow.com/questions/9628273/libgfortran-version-gfortran-1-4-not-found, things work out

            I’m not sure only step2 is OK for this problem. If you tried out, let me know.

        • Steve

          I had a similar problem that I fought with for awhile then I remembered that I had commented out one particular line that appears to have been the problem (because it is now working):

          dlopen(“libpython2.7.so”, RTLD_LAZY |RTLD_GLOBAL);

          I think I commented it out initially because I ran into problems with the call to dlopen.

          If this doesn’t work for you I’d suggest trying to
          import numpy in a simple cpp file. Something like:

          // =======================================

          #include
          #include

          using namespace std;

          int main(int argc, char *argv[])
          {

          // Initialize the Python Interpreter
          Py_Initialize();

          // import numpy // Initialize the Python Interpreter
          PyObject *numpy = PyImport_ImportModule(“numpy”);

          if(numpy == NULL){
          cout << "Import Errorn";
          }else{
          cout << "Numpy importedn";
          }

          // Finish the Python Interpreter
          Py_Finalize();

          return 0;
          }
          // =======================================
          Then if you can get that to work you could wrap similar code in a mex function and see if you can get that to compile in matlab. BTW, I think the gcc call for the above call is something like:

          g++ numpy_import_test.cpp -lpython2.7 -o numpy_import_test

          Given that you can get it to compile, see if you can actually run it or if you get a similar error. If not then there is some goofiness in the MATLAB call to g++.

          I want to also say thank you to the admin on this one. This is a great implementation, a nicer version than pymex, quicker than a jython call. I'd urge you to put it on the file exchange, it's somewhat difficult to find.

          • Steve

            sorry the include statements were interpreted as html code, you should include
            python2.7/Python.h
            and
            iostream

        • Andres

          Hey, I’ve had esentially the same problems here on Ubuntu 12.10.
          I cannot get this to work. But it works on Mac OSX Mountain Lion.

          I tried running Matlab with strace, but this option is only available for simulink modules.
          Admin: how do you run this with strace? sorry for the beginner question.

          What versions and distros do you run this on? and what versions of Matlab?

          Thanks

          • admin

            Sorry I think this is more Python embedding related problem than matpy-related problem. The best place to seek advice would be a Python forum :(

  4. Shiquan Wang

    Hi, why not host your code in something like github, sourceforge, etc..
    So that your code can make bigger difference and as a base to move forward to features like mlabwrap.

    • admin

      I can’t see how it fails to make a difference here 😉 It’s BSD-licensed. You’re welcome to setup a github repository yourself :)

  5. Cres

    Hi,

    Firstly I would like to thank you for your work.

    I ran into some errors compiling the mex function in Windows 64, on both 32 bit and 64 bit versions of matlab 2012b with the Visual C++ 2010 compiler and the -largeArrayDims option.

    py.cpp
    py.cpp(65) : error C2440: ‘initializing’ : cannot convert from ‘const mwSize *’ to ‘const int *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(129) : warning C4244: ‘argument’ : conversion from ‘__int64’ to ‘double’, possible loss of data
    py.cpp(129) : warning C4244: ‘argument’ : conversion from ‘__int64’ to ‘double’, possible loss of data
    py.cpp(130) : warning C4244: ‘argument’ : conversion from ‘unsigned __int64’ to ‘double’, possible loss of data
    py.cpp(130) : warning C4244: ‘argument’ : conversion from ‘unsigned __int64’ to ‘double’, possible loss of data
    py.cpp(182) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int [2]’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(183) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int [2]’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(183) : warning C4800: ‘long’ : forcing value to bool ‘true’ or ‘false’ (performance warning)
    py.cpp(184) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int [2]’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(185) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int [2]’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(188) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int [2]’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(245) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(245) : warning C4800: ‘long’ : forcing value to bool ‘true’ or ‘false’ (performance warning)
    py.cpp(246) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(247) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(248) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    py.cpp(249) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(250) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(251) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(252) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(253) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(254) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(255) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(270) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(271) : error C2664: ‘mxCreateNumericArray_730’ : cannot convert parameter 2 from ‘int *’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    py.cpp(284) : error C2664: ‘mxCreateCellArray_730’ : cannot convert parameter 2 from ‘int [2]’ to ‘const mwSize *’

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    E:MATLAB~2BINMEX.PL: Error: Compile of ‘py.cpp’ failed.

    It seems like the code is trying to change “pointer to a constant integer” to simply a “pointer to integer”. It might be compiler specific to allow such a conversion (without an explicit cast)?

    So which version of windows, matlab and compiler did you test it on?

    Thanks,

    Cres

  6. Dan

    Hi,
    Pardon the novice question, but I’m having trouble getting this installed. I’m running MATLAB R2011a on Mac OS X 10.8.

    I type
    >> mex py.cpp -I/Library/Python/2.7/

    and it returns the following error message:
    /Applications/MATLAB_R2011a.app/bin/mex: line 305: gcc-4.2: command not found

    mex: py.cpp not a normal file or does not exist.

    ??? Error using ==> mex at 208
    Unable to complete successfully.

  7. Dan

    OK, I’ve encountered another problem on Mac OS X. As you probably know, OS X ships with a built-in version of Python, but the community consensus seems to be that one should not use this version but should instead get Python from python.org. OK, so I have been using the official python.org distribution for everything, and all of my modules are installed there.

    When I compile with
    mex py.cpp -lpython2.7 -ldl
    the compiler runs, but it uses the system version of python, not the python.org version. As a result, all of the modules I care about are missing. So, somehow I need to force matpy to compile with the correct version of Python.

    I tried
    mex py.cpp -l/Library/Frameworks/Python.framework/Versions/2.7/bin/python
    but it returns the error
    ld: library not found for -l/Library/Frameworks/Python.framework/Versions/2.7/bin

    I also tried
    mex py.cpp -L/Library/Frameworks/Python.framework/Versions/2.7/
    which returns the following long error:

    Undefined symbols for architecture x86_64:
    “_PyArg_ParseTuple”, referenced from:
    aview_write(_object*, _object*)in py.o
    “_PyBool_FromLong”, referenced from:
    mat2py(mxArray_tag const*) in py.o
    “_PyBool_Type”, referenced from:
    py2mat(_object*) in py.o
    “_PyComplex_FromDoubles”, referenced from:
    mat2py(mxArray_tag const*) in py.o
    “_PyComplex_ImagAsDouble”, referenced from:
    py2mat(_object*) in py.o
    “_PyComplex_RealAsDouble”, referenced from:
    py2mat(_object*) in py.o
    “_PyComplex_Type”, referenced from:
    py2mat(_object*) in py.o
    “_PyDict_GetItemString”, referenced from:
    _mexFunction in py.o
    “_PyDict_New”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyDict_SetItemString”, referenced from:
    _mexFunction in py.o
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyErr_Print”, referenced from:
    _mexFunction in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyEval_EvalCode”, referenced from:
    _mexFunction in py.o
    “_PyFloat_AsDouble”, referenced from:
    py2mat(_object*) in py.o
    “_PyFloat_FromDouble”, referenced from:
    mat2py(mxArray_tag const*) in py.o
    “_PyFloat_Type”, referenced from:
    py2mat(_object*) in py.o
    “_PyImport_AddModule”, referenced from:
    _mexFunction in py.o
    “_PyImport_ImportModule”, referenced from:
    _mexFunction in py.o
    “_PyInt_AsLong”, referenced from:
    py2mat(_object*) in py.o
    “_PyInt_AsUnsignedLongMask”, referenced from:
    py2mat(_object*) in py.o
    “_PyInt_FromLong”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyList_GetItem”, referenced from:
    py2mat(_object*) in py.o
    “_PyList_New”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyList_SetItem”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyLong_AsLongLong”, referenced from:
    py2mat(_object*) in py.o
    “_PyLong_AsUnsignedLongLong”, referenced from:
    py2mat(_object*) in py.o
    “_PyLong_FromLongLong”, referenced from:
    mat2py(mxArray_tag const*) in py.o
    “_PyLong_FromUnsignedLongLong”, referenced from:
    mat2py(mxArray_tag const*) in py.o
    “_PyModule_GetDict”, referenced from:
    _mexFunction in py.o
    “_PyObject_Call”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyObject_CallMethod”, referenced from:
    py2mat(_object*) in py.o
    “_PyObject_GetAttrString”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyObject_IsInstance”, referenced from:
    py2mat(_object*) in py.o
    “_PyObject_Str”, referenced from:
    py2mat(_object*) in py.o
    “_PyRun_StringFlags”, referenced from:
    _mexFunction in py.o
    “_PySequence_Check”, referenced from:
    py2mat(_object*) in py.o
    “_PySequence_GetItem”, referenced from:
    py2mat(_object*) in py.o
    “_PySequence_Size”, referenced from:
    py2mat(_object*) in py.o
    “_PyString_AsString”, referenced from:
    py2mat(_object*) in py.o
    “_PyString_FromString”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PySys_SetObject”, referenced from:
    _mexFunction in py.o
    “_PyTuple_New”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyTuple_SetItem”, referenced from:
    py2mat(_object*) in py.o
    mat2py(mxArray_tag const*) in py.o
    “_PyType_IsSubtype”, referenced from:
    py2mat(_object*) in py.o
    “_Py_BuildValue”, referenced from:
    aview_write(_object*, _object*)in py.o
    “_Py_CompileStringFlags”, referenced from:
    _mexFunction in py.o
    “_Py_InitModule4_64”, referenced from:
    _mexFunction in py.o
    “_Py_Initialize”, referenced from:
    _mexFunction in py.o
    ld: symbol(s) not found for architecture x86_64

    Any help would be much appreciated!
    Thanks,
    Dan

    • admin

      You should read what -L and -l mex options do first.

      The first one specifies path to the directory with libraries, the second one actually links against a library so you should do mex [….] -L/path/to/dir/containing/libpython_so -lpython2.7

      Also, you might want to point to the directory with correct headers for your custom python version using -I/path/to/dir/containing/Python_h

      • Dan

        Thanks – I did read the Matlab documentation about -L and -l, but I found it rather opaque. Your explanation is clearer. It compiled successfully (and used the correct version of python) when I called
        mex py.cpp -L/Library/Frameworks/Python.framework/Versions/2.7/lib -lpython2.7

  8. Dan

    My apologies for the repeated posts… However, I think I may have found a bug. I have the numpy array x which was generated by a python module that I ran. py(‘eval’,’print(x)’) returns the following:
    [ 6 6 4 5 4 4 4 3 2 1 2 3 8 3 10 6 9 9 7 8 6 5 8 7 12
    6 6 4 5 4 4 4 3 2 1 2 3 8 3 10 6 6 4 5 4 4 4 3 2 1
    2 3 8 3 10 6 9 9 7 8 6 5 8 7 12]

    Now, I get these data into MATLAB with x = py(‘get’,’x’);
    So far so good. But then I pass the data back to Python with py(‘set’,’x2′,x);
    And py(‘eval’,’print(x2)’) returns this:
    [[ 6]
    [ 6]
    [ 4]
    [ 5]
    [ 4]
    [ 4]
    [ 4]
    [ 3]
    [ 2]
    [ 1]
    [ 2]
    [ 3]
    [ 8]
    [ 3]
    [10]
    [ 6]
    [ 9]
    [ 9]
    [ 7]
    [ 8]
    [ 6]
    [ 5]
    [ 8]
    [ 7]
    [12]
    [ 6]
    [ 6]
    [ 4]
    [ 5]
    [ 4]
    [ 4]
    [ 4]
    [ 3]
    [ 2]
    [ 1]
    [ 2]
    [ 3]
    [ 8]
    [ 3]
    [10]
    [ 6]
    [ 6]
    [ 4]
    [ 5]
    [ 4]
    [ 4]
    [ 4]
    [ 3]
    [ 2]
    [ 1]
    [ 2]
    [ 3]
    [ 8]
    [ 3]
    [10]
    [ 6]
    [ 9]
    [ 9]
    [ 7]
    [ 8]
    [ 6]
    [ 5]
    [ 8]
    [ 7]
    [12]]

    I can easily convert these data back to their original appearance using x2 = numpy.concatenate(x2), so this is not a fatal flaw. However, I’m a little curious why it’s happening and whether it could be avoided by changing my syntax.

    Thanks,
    Dan

    • admin

      It transposes the vector. It is related to the fact that MATLAB stores matrices in column-major order. Therefore it is better to store vectors in vertical format also in Python (numpy). Then no conversion will occur.

    • admin

      More precisely – the first dimension in MATLAB is vertical. Therefore when you take a one-dimensional vector from Python it is vertical in MATLAB and since MATLAB doesn’t have one-dimensional matrices, just 2D with singleton dimension, it gets re-exported as vertical N rows 1 column np.array.

      • Dan

        OK, what you said makes sense. But it seems to me that this behavior reveals a design flaw in matpy. Suppose I generate a 1-d array (i.e. a vector) of data from some process in MATLAB, and now I want to pass it to python and do something with it. Shouldn’t py(‘set’) give a python object that behaves as close as possible to the original variable in MATLAB? That would be a python list or 1-d np.array.

        Or to put it a different way, if conversion of variables is “1:1” (as you claim above), then shouldn’t I be able to use data in python in exactly the format in which it is imported, rather than having to first convert it (using something like concatenate)?

        I don’t mean to be overly critical, as I think you’ve made a great tool – just making a small suggestion for improvement (or at least, a suggestion for better documentation of how py(‘set’) behaves!).

        • admin

          I will repeat – there are no 1-d objects in MATLAB, the smallest dimensionality is 2-d which can have a singleton dimension. They’re converted 1:1 if you start from MATLAB. I have no way of determining whether I should convert 1-d object from Python to 1xN or Nx1 vector in MATLAB. This is an arbitrary decision. I chose to convert it to Nx1 vector, you can modify the code to convert it to 1xN vector if you prefer that. Alternatively when you pass it back to Python you can simply do py(‘set’, ‘x2′, x’)

  9. Jeff

    Hi there,
    First, thanks for providing the code and taking the time to develop it. I’m having a novice problem (novice to both mex and Python, I’m afraid, though not to MATLAB). When I run the following code to compile the function I get a string of errors. Any ideas/suggestions to get me on track? The version of Python I’m using shipped with ArcGIS. Hence the odd installation directory. But it works fine in the command prompt and through Spyder. Running windows7 x64 and 64 bit Matlab 2010b.

    Many thanks again.

    >> mex py.cpp -IC:/Python27/ArcGIS10.1/include C:/Python27/ArcGIS10.1/libs/python27.lib
    Creating library C:UsersJeffreyAppDataLocalTempmex_spMVQ4templib.x and object C:UsersJeffreyAppDataLocalTempmex_spMVQ4templib.exp
    py.obj : error LNK2019: unresolved external symbol __imp_Py_BuildValue referenced in function “struct _object * __cdecl aview_write(struct _object *,struct _object *)” (?aview_write@@YAPEAU_object@@PEAU1@0@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyArg_ParseTuple referenced in function “struct _object * __cdecl aview_write(struct _object *,struct _object *)” (?aview_write@@YAPEAU_object@@PEAU1@0@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySys_SetObject referenced in function “void __cdecl initaview(void)” (?initaview@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_Py_InitModule4_64 referenced in function “void __cdecl initaview(void)” (?initaview@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_GetAttrString referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_Call referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyDict_SetItemString referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyDict_New referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyTuple_SetItem referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyTuple_New referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_FromDoubles referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_FromUnsignedLongLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_FromLongLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyInt_FromLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyFloat_FromDouble referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyErr_Print referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyBool_FromLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyList_SetItem referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyList_New referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyString_FromString referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySequence_Check referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_ImagAsDouble referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_RealAsDouble referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_AsUnsignedLongLong referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyInt_AsUnsignedLongMask referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyFloat_AsDouble referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyList_GetItem referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_CallMethod referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySequence_GetItem referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySequence_Size referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_Str referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_IsInstance referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyString_AsString referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_Type referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyType_IsSubtype referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyFloat_Type referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_AsLongLong referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyBool_Type referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyInt_AsLong referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyEval_EvalCode referenced in function “void __cdecl do_get(void)” (?do_get@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_Py_CompileStringFlags referenced in function “void __cdecl do_get(void)” (?do_get@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_PyRun_StringFlags referenced in function “void __cdecl do_eval(void)” (?do_eval@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_PyDict_GetItemString referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_PyImport_ImportModule referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_PyModule_GetDict referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_PyImport_AddModule referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_Py_Initialize referenced in function mexFunction
    py.mexw64 : fatal error LNK1120: 47 unresolved externals

    C:PROGRA~1MATLABR2010BBINMEX.PL: Error: Link of ‘py.mexw64’ failed.

    ??? Error using ==> mex at 208
    Unable to complete successfully.

    • admin

      Actually this is quite bizarre. It means that all those symbols were not found in the given Python library. Just guessing – maybe ArcGIS is using 32-bit version of Python? Or Python compiled using something else than Microsoft Visual Studio? Are you using Visual Studio to compile mex files?

      • Jeff

        Thanks for getting back to me. Yes, I believe that it’s 32 bit Python:
        Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
        Type “help”, “copyright”, “credits” or “license” for more information.

        It seems that desktop ArcGIS requires 32 bit python. Since I initially started learning/using Python a month ago to access ArcGIS scripting, I need to make sure that I leave that functionality intact.

        Do you think there’s a way to get your matpy function to compile correctly given this limitation? What about a separate install of 64 bit Python in a different directory? Or would that break other things in my system (e.g. Spyder, or maybe ArcGIS – I realize these questions might be off topic).

        Thanks again.

        • admin

          64-bit MEX files cannot be linked with 32-bit libraries. Yes, separate installation of 64-bit Python should help, however, ArcGIS won’t be accessible through matpy linked against such 64-bit Python installation. It shouldn’t break anything else though.

  10. Marmaduke

    Hi

    I had need for a lower level approach to interface MATLAB to some numerical code, and I’ve bootstrapped the mexFunction and all in Cython, and some of the details you provided here were essential, e.g. the call to dlopen(). Thanks!

    Now that it is working, I have noticed that calling linalg routines ends up calling into MATLAB’s MKL libraries, resulting in errors. In my main mexFunction, I’ve got a switch like


    if arg1 == 'np1':
    import numpy
    print numpy.random.randn(3)
    elif arg1 == 'np2':
    from numpy import linalg, r_
    A = r_[:20].reshape((4, 5))
    print linalg.svd(A)[1]

    and in a MATLAB session, I see


    >> cymex np1
    [-02.392350, 2.323590235, -02350.035]

    >> cymex np2
    MKL ERROR: Parameter 5 was incorrect on entry to DGESDD

    [0. 0. 0. 0.]

    Have you any idea how to manage the linking of the different libraries? Is it necessary to use a NumPy that has been compiled against MKL?

    Thanks again for the nice work!

    • Marmaduke

      Sorry for the formatting; those HTML tags don’t seem to work. Please take a look at the linked gist for the actual code.

      • admin

        I suspect a shared library conflict between numpy and MATLAB that doesn’t necessarily have to originate from MKL library itself, maybe from one of the common dependencies of MKL and alternative linear algebra implementation used by numpy. I think you could use step by step debugging in numpy (compiled with debugging symbols) to nail it down. Octave probably doesn’t use MKL, hence – no problems.

        • Carlos Ortiz

          Hey guys,

          Running Ubuntu 13.10, 64-bit, MATLAB R2013b

          I contacted Tech Support at MATLAB and here is how they helped me solve the MKL issue.

          Compile py.cpp making sure the code execudes the dlopen command for libpython

          mex py.cpp -g -v -I/usr/include/python2.7/ -L/usr/lib -DLIBPYTHON=/usr/lib/x86_64-linux-gnu/libpython2.7.so -lpython2.7 -ldl LDFLAGS=’$LDFLAGS -Xlinker -export-dynamic’ -compatibleArrayDims

          export environment variables
          export LAPACK_VERBOSITY=1
          export LAPACK_VERSION=/usr/lib/lapack/liblapack.so.3
          export FFTW_VERSION=/usr/lib/x86_64-linux-gnu/libfftw3.so.3 #libfftw3i.so,libfftw3f.so
          export BLAS_VERSION=/usr/lib/libblas.so.3

          run matlab
          LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/lib/x86_64-linux-gnu/libgcc_s.so.1:/usr/lib/x86_64-linux-gnu/libgfortran.so.3:/usr/lib/libblas.so.3:/usr/lib/lapack/liblapack.so.3 matlab &

          The MKL error is because MATLAB uses the Intel version of LAPACK and python uses the system-wide version which is different.

          You can verify that you fixed things by executing in matlab:
          version -lapack
          version -blas
          It should say MKL before setting the env variables, and something else after. For me, the BLAS version is not recognized after the procedure, but everything seems to be working fine.

          Here’s to hoping that generating python plots from matlab was totally worth the 8 hours I just spent on this!

          Best,
          Carlos

          • admin

            Many thanks, Carlos. A very useful lesson from the never-ending encyclopedia of DLL hell. Also cool that MATLAB guys treated it seriously. All the best!

  11. anon

    I saw pymex referred to above but I think there is more than one. If people are not aware of it I think this is excellent:
    https://github.com/kw/pymex

    It works well (there is a fork which builds on windows) and is a bit more sophisticated – it takes care of boxing objects so you can basically use Matlab as a python environment (all your python stuff is proxied as Matlab objects and can be manipulated naturally within Matlab).

    • admin

      Lol. Where was it when I was looking for it? 😉 When matpy becomes insufficient for my needs, I think I will give it a try. Thanks. Boxing however is really not a priority, I think it only harms performance. It’s better to exchange variables and do your thing in big chunks of code. The functionality of copying current workspace to Python, then calling Python and copying back the results (I think I’ve spotted this in pymex) is useful – I was thinking about it myself, but didn’t have time to implement it.

  12. Derek Dalle

    Do you have any thoughts on getting interactive mode to work with matplotlib? Currently I got it working in R2011a on Linux much like your example, but the window buttons don’t function if I have run ‘ion()’. It’s still pretty functional because issuing a ‘draw()’ command still updates the figure, but people might want to zoom and/or use the save button from the figure window.

    I suppose this will require threading but don’t really know how to do it.

    • admin

      Hmm… but does ion() work for you in Python? My impression is that people keep complaining about it even when running native Python interpreter.

      • Derek Dalle

        Yes, ion() works as expected for me in both a normal Python shell and IPython. Also, it mostly works in matpy here. If I do something like the following …

        >> X = linspace(0, 2*pi, 101);
        >> Y = sin(X);
        >> py_export(‘X’, ‘Y’)
        >> py_shell
        py> from pylab import *
        py> ion()
        py> h = plot(X[0], Y[0])
        py> draw()
        py> setp(h[0], ‘c’, ‘r’, ‘lw’, 2)
        py> draw()

        It draws the figure as expected on both draw() commands (first as a blue curve and then red and thicker). It’s just that the resulting window seems to have no way to obtain input from the mouse.

        • Carlos Ortiz

          I can confirm that on my recent installation the drawn figure responds to the mouse, even for 3D graphics. Everything just works. Saving works, zooming works…

  13. jackpot5

    казино рулетка игры бесплатно еще онлайн казино с выводом денег.

  14. Jed

    Thanks for the nice package. I find it useful.

    I would like to be able to add debugging in my python code using pdb.set_trace(). This uses raw_input, which does not seem compatible with matpy. Have you thought about how hard it might be to allow raw_input commands to work with matpy?

  15. Marco

    I am using your package above. I modified a .py file but when I call it from Matlab it seems to call the old version instead. I import the module and reload it in Matlab but to no avail.

  16. Flo

    Hi,

    I’m trying to instal MatPy on my windows 7 machine, running Matlab 2013 64 bit.

    When I try the command :

    mex py.cpp -IC:/Python27/include C:/Python27/libs/python27.lib

    I get :

    ———————————————————-
    Creating library C:UsersfqlAppDataLocalTemp1mex_seAMAFtemplib.x and object C:UsersfqlAppDataLocalTemp1mex_seAMAFtemplib.exp
    py.obj : error LNK2019: unresolved external symbol __imp_Py_BuildValue referenced in function “struct _object * __cdecl aview_write(struct _object *,struct _object *)” (?aview_write@@YAPEAU_object@@PEAU1@0@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyArg_ParseTuple referenced in function “struct _object * __cdecl aview_write(struct _object *,struct _object *)” (?aview_write@@YAPEAU_object@@PEAU1@0@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySys_SetObject referenced in function “void __cdecl initaview(void)” (?initaview@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_Py_InitModule4_64 referenced in function “void __cdecl initaview(void)” (?initaview@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_GetAttrString referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_Call referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyDict_SetItemString referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyDict_New referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyTuple_SetItem referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyTuple_New referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_FromDoubles referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_FromUnsignedLongLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_FromLongLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyInt_FromLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyFloat_FromDouble referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyErr_Print referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyBool_FromLong referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyList_SetItem referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyList_New referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyString_FromString referenced in function “struct _object * __cdecl mat2py(struct mxArray_tag const *)” (?mat2py@@YAPEAU_object@@PEBUmxArray_tag@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySequence_Check referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_ImagAsDouble referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_RealAsDouble referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_AsUnsignedLongLong referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyInt_AsUnsignedLongMask referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyFloat_AsDouble referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyList_GetItem referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_CallMethod referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySequence_GetItem referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PySequence_Size referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_Str referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyObject_IsInstance referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyString_AsString referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyComplex_Type referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyType_IsSubtype referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyFloat_Type referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyLong_AsLongLong referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyBool_Type referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyInt_AsLong referenced in function “struct mxArray_tag * __cdecl py2mat(struct _object *)” (?py2mat@@YAPEAUmxArray_tag@@PEAU_object@@@Z)
    py.obj : error LNK2019: unresolved external symbol __imp_PyEval_EvalCode referenced in function “void __cdecl do_get(void)” (?do_get@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_Py_CompileStringFlags referenced in function “void __cdecl do_get(void)” (?do_get@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_PyRun_StringFlags referenced in function “void __cdecl do_eval(void)” (?do_eval@@YAXXZ)
    py.obj : error LNK2019: unresolved external symbol __imp_PyDict_GetItemString referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_PyImport_ImportModule referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_PyModule_GetDict referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_PyImport_AddModule referenced in function mexFunction
    py.obj : error LNK2019: unresolved external symbol __imp_Py_Initialize referenced in function mexFunction
    py.mexw64 : fatal error LNK1120: 47 unresolved externals

    ————————————————-

    any idea hoe to solve “error LNK2019”

    Thanks a lot !

  17. Thn

    Hello,

    First of all many thanks for all your hard work on creating this, and maintaining it answering questions.

    I have the same problem as reported by Flo (30 June 2013) and Jeff (2 January 2013), i.e.

    LINK : warning LNK4044: unrecognized option ‘/lkernel32’; ignored
    Creating library C:UsersTHANAS~1AppDataLocalTempmex_IxYcXmtemplib.x and object C:UsersTHANAS~1AppDataLocalTempmex_IxYcXmtemplib.exp
    py.obj : error LNK2019: unresolved external symbol __imp_Py_BuildValue referenced in function “struct _object * __cdecl aview_write(struct _object *,struct _object *)” (?aview_write@@YAPEAU_object@@PEAU1@0@Z)

    and so on for 46 more cases where the LNK2019 error appears.

    I am using windows7 x64 and 64 bit Matlab 2010b.

    Has anyone else had this – Jeff in particular if you read this, did you manage to solve the problem?

    Many thanks

  18. Dylan Muir

    Hi,

    Is there a nice way to reset the python interpreter without restarting matlab? I tried modifying your code to add a new “reset” command, which called “Py_Finalize” before setting thing up again with your call to “Py_Initialize”, but only got segmentation faults.

    Thanks,
    D.

    • admin

      Why do you need to do that? Py_Finalize() has some bugs, maybe that’s the reason. Maybe you can provide a stack trace as well, this would help to determine the cause.

  19. Dylan Muir

    Here’s the trace:

    ————————————————————————
    Segmentation violation detected at Fri Sep 20 20:16:33 2013
    ————————————————————————

    Configuration:
    MATLAB Version: 7.11.0.584 (R2010b)
    MATLAB License: 161051
    Operating System: Darwin 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
    Window System: No active display
    Current Visual: None
    Processor ID: x86 Family 6 Model 10 Stepping 9, GenuineIntel
    Virtual Machine: Java 1.6.0_51-b11-457-11M4509 with Apple Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
    Default Encoding: ISO-8859-1

    Fault Count: 1

    Abnormal termination:

    Register State (from fault):
    RAX = 000000011315dde0 RBX = 000000012be13af1
    RCX = 000000011315de10 RDX = 000000012be8d560
    RSP = 000000012be9ccc0 RBP = 00000001245073f0
    RSI = 000000012be13c95 RDI = 000000011315de00

    R8 = 000000011315de30 R9 = 000000012bd6197d
    R10 = 000000011315df10 R11 = 000000012be9ccc0
    R12 = 000000011315df10 R13 = 000000012be9bf60
    R14 = 000000011315de70 R15 = 000000012be1799e

    RIP = 000000011315de70 RFL = 000000012be9ccc0

    CS = 000000011315df10 FS = 000000012be9bf60 GS = 000000011315e3e0

    Stack Trace (from fault):
    [ 0] 0x0000000100154109 /Applications/MATLAB_R2010b.app/bin/maci64/libmwfl.dylib+00409865 fl::sysdep::osx::unwind_stack(void const**, unsigned long, unsigned long, fl::diag::thread_context const&)+000009
    [ 1] 0x00000001000f803f /Applications/MATLAB_R2010b.app/bin/maci64/libmwfl.dylib+00032831 fl::diag::stacktrace_base::capture(fl::diag::thread_context const&, unsigned long)+000063
    [ 2] 0x00000001001053e4 /Applications/MATLAB_R2010b.app/bin/maci64/libmwfl.dylib+00087012 fl::util::atomic_singleton<(anonymous namespace)::terminate_impl, fl::util::default_ctor_placement >::create()+003156
    [ 3] 0x0000000100103e17 /Applications/MATLAB_R2010b.app/bin/maci64/libmwfl.dylib+00081431 fl::diag::terminate_log(char const*, fl::diag::thread_context const&, bool)+000199
    [ 4] 0x00000001005213d2 /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00267218 mnSetCmdLineArg+000834
    [ 5] 0x00007fff8ee3194a /usr/lib/system/libsystem_c.dylib+00010570 _sigtramp+000026
    [ 6] 0x000000012c064310 /usr/lib/system/libsystem_c.dylib+18446603343152501520
    [ 7] 0x000000012be1799e /Library/Frameworks/Python.framework/Versions/2.7/Python+01075614 initposix+000174
    [ 8] 0x000000012bdebb3f /Library/Frameworks/Python.framework/Versions/2.7/Python+00895807 init_builtin+000111
    [ 9] 0x000000012bdecf66 /Library/Frameworks/Python.framework/Versions/2.7/Python+00900966 load_module+000470
    [ 10] 0x000000012bded32f /Library/Frameworks/Python.framework/Versions/2.7/Python+00901935 import_submodule+000383
    [ 11] 0x000000012bded84a /Library/Frameworks/Python.framework/Versions/2.7/Python+00903242 load_next+000234
    [ 12] 0x000000012bdee2ab /Library/Frameworks/Python.framework/Versions/2.7/Python+00905899 PyImport_ImportModuleLevel+000363
    [ 13] 0x000000012bdcbfa3 /Library/Frameworks/Python.framework/Versions/2.7/Python+00765859 builtin___import__+000131
    [ 14] 0x000000012bd19e12 /Library/Frameworks/Python.framework/Versions/2.7/Python+00036370 PyObject_Call+000098
    [ 15] 0x000000012bdcc4e7 /Library/Frameworks/Python.framework/Versions/2.7/Python+00767207 PyEval_CallObjectWithKeywords+000087
    [ 16] 0x000000012bdd12c6 /Library/Frameworks/Python.framework/Versions/2.7/Python+00787142 PyEval_EvalFrameEx+015142
    [ 17] 0x000000012bdd5ad6 /Library/Frameworks/Python.framework/Versions/2.7/Python+00805590 PyEval_EvalCodeEx+002118
    [ 18] 0x000000012bdd5bf6 /Library/Frameworks/Python.framework/Versions/2.7/Python+00805878 PyEval_EvalCode+000054
    [ 19] 0x000000012bdebcf0 /Library/Frameworks/Python.framework/Versions/2.7/Python+00896240 PyImport_ExecCodeModuleEx+000208
    [ 20] 0x000000012bdeca12 /Library/Frameworks/Python.framework/Versions/2.7/Python+00899602 load_source_module+000722
    [ 21] 0x000000012bded32f /Library/Frameworks/Python.framework/Versions/2.7/Python+00901935 import_submodule+000383
    [ 22] 0x000000012bded84a /Library/Frameworks/Python.framework/Versions/2.7/Python+00903242 load_next+000234
    [ 23] 0x000000012bdee2ab /Library/Frameworks/Python.framework/Versions/2.7/Python+00905899 PyImport_ImportModuleLevel+000363
    [ 24] 0x000000012bdcbfa3 /Library/Frameworks/Python.framework/Versions/2.7/Python+00765859 builtin___import__+000131
    [ 25] 0x000000012bd19e12 /Library/Frameworks/Python.framework/Versions/2.7/Python+00036370 PyObject_Call+000098
    [ 26] 0x000000012bdcc4e7 /Library/Frameworks/Python.framework/Versions/2.7/Python+00767207 PyEval_CallObjectWithKeywords+000087
    [ 27] 0x000000012bdd12c6 /Library/Frameworks/Python.framework/Versions/2.7/Python+00787142 PyEval_EvalFrameEx+015142
    [ 28] 0x000000012bdd5ad6 /Library/Frameworks/Python.framework/Versions/2.7/Python+00805590 PyEval_EvalCodeEx+002118
    [ 29] 0x000000012bdd5bf6 /Library/Frameworks/Python.framework/Versions/2.7/Python+00805878 PyEval_EvalCode+000054
    [ 30] 0x000000012bdebcf0 /Library/Frameworks/Python.framework/Versions/2.7/Python+00896240 PyImport_ExecCodeModuleEx+000208
    [ 31] 0x000000012bdeca12 /Library/Frameworks/Python.framework/Versions/2.7/Python+00899602 load_source_module+000722
    [ 32] 0x000000012bded32f /Library/Frameworks/Python.framework/Versions/2.7/Python+00901935 import_submodule+000383
    [ 33] 0x000000012bded84a /Library/Frameworks/Python.framework/Versions/2.7/Python+00903242 load_next+000234
    [ 34] 0x000000012bdee2ab /Library/Frameworks/Python.framework/Versions/2.7/Python+00905899 PyImport_ImportModuleLevel+000363
    [ 35] 0x000000012bdcbfa3 /Library/Frameworks/Python.framework/Versions/2.7/Python+00765859 builtin___import__+000131
    [ 36] 0x000000012bd19e12 /Library/Frameworks/Python.framework/Versions/2.7/Python+00036370 PyObject_Call+000098
    [ 37] 0x000000012bd1f1e5 /Library/Frameworks/Python.framework/Versions/2.7/Python+00057829 PyObject_CallFunction+000197
    [ 38] 0x000000012bdeea4b /Library/Frameworks/Python.framework/Versions/2.7/Python+00907851 PyImport_Import+000187
    [ 39] 0x000000012bdeebf8 /Library/Frameworks/Python.framework/Versions/2.7/Python+00908280 PyImport_ImportModule+000040
    [ 40] 0x000000012bdfc6cc /Library/Frameworks/Python.framework/Versions/2.7/Python+00964300 Py_InitializeEx+001324
    [ 41] 0x0000000104fb26ef /Users/dylan/Documents/Personal Documents/Research/Mrsic-Flogel group/Projects/RF similarity and connectivity/GIT_LSCSM/Python/matpy/py.mexmaci64+00005871 mexFunction+000143
    [ 42] 0x0000000102a634da /Applications/MATLAB_R2010b.app/bin/maci64/libmex.dylib+00021722 mexRunMexFile+000090
    [ 43] 0x0000000102a60ef9 /Applications/MATLAB_R2010b.app/bin/maci64/libmex.dylib+00012025 __constructor_lm_mexmaci64+000553
    [ 44] 0x0000000102a61094 /Applications/MATLAB_R2010b.app/bin/maci64/libmex.dylib+00012436 __constructor_lm_mexmaci64+000964
    [ 45] 0x00000001005ab881 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_dispatcher.dylib+00260225 Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**)+000321
    [ 46] 0x0000000100712643 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+01144387 inValidateLoadedObject(mxArray_tag*)+001779
    [ 47] 0x00000001006af7b0 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00739248 QueryMLFcnTable_m_interpreter+012000
    [ 48] 0x00000001006c6340 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00832320 MathWorks::System::FatalException::~FatalException()+024080
    [ 49] 0x00000001006cb2dc /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00852700 MathWorks::System::FatalException::~FatalException()+044460
    [ 50] 0x00000001006c9059 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00843865 MathWorks::System::FatalException::~FatalException()+035625
    [ 51] 0x00000001006c9a44 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00846404 MathWorks::System::FatalException::~FatalException()+038164
    [ 52] 0x0000000100729a0a /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+01239562 inRunLoadObjFunction(mxArray_tag*)+093098
    [ 53] 0x00000001005ab881 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_dispatcher.dylib+00260225 Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**)+000321
    [ 54] 0x00000001007070f3 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+01097971 inPathNotification::~inPathNotification()+033395
    [ 55] 0x00000001006bf4fe /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00804094 unsigned long mdApplyToFunctions(md_match_plmmf, bool (*)(Mfh_MATLAB_fn const*), md_apply_push_pfhmf)+025614
    [ 56] 0x00000001006bff91 /Applications/MATLAB_R2010b.app/bin/maci64/libmwm_interpreter.dylib+00806801 unsigned long mdApplyToFunctions(md_match_plmmf, bool (*)(Mfh_MATLAB_fn const*), md_apply_push_pfhmf)+028321
    [ 57] 0x00000001004bafae /Applications/MATLAB_R2010b.app/bin/maci64/libmwbridge.dylib+00044974 mnInitializeParser(char const*, char const**)+000238
    [ 58] 0x00000001004bbaa4 /Applications/MATLAB_R2010b.app/bin/maci64/libmwbridge.dylib+00047780 mnParser+000724
    [ 59] 0x000000010050b329 /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00176937 mcrInstance::mnParser_on_interpreter_thread()+000041
    [ 60] 0x00000001004f29ef /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00076271 MathWorks::System::SimpleException::~SimpleException()+001535
    [ 61] 0x00000001004eda79 /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00055929 mvm::detail::RecursiveScopedMcrLock::~RecursiveScopedMcrLock()+002281
    [ 62] 0x0000000100288a15 /Applications/MATLAB_R2010b.app/bin/maci64/libmwservices.dylib+00109077 eventqueue::UserEventQueue::pop()+000069
    [ 63] 0x0000000100288461 /Applications/MATLAB_R2010b.app/bin/maci64/libmwservices.dylib+00107617 eventqueue::UserEventQueue::removeInput(int)+000337
    [ 64] 0x000000010451e73c /Applications/MATLAB_R2010b.app/bin/maci64/libmwuix.dylib+00292668 sysq::ws_ppeHook::pollingPostFcn(int)+000700
    [ 65] 0x00000001043f0d1a /Applications/MATLAB_R2010b.app/bin/maci64/libuij.dylib+00146714 sysq::ws_ppeHook::pollingPostFcn(int)+004330
    [ 66] 0x00000001043f171c /Applications/MATLAB_R2010b.app/bin/maci64/libuij.dylib+00149276 sysq::ws_ppeHook::pollingPostFcn(int)+006892
    [ 67] 0x00000001043eed67 /Applications/MATLAB_R2010b.app/bin/maci64/libuij.dylib+00138599 svWS_ProcessPendingEvents(int, int, bool)+000455
    [ 68] 0x00000001004ef1f5 /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00061941 mvm::detail::RecursiveScopedMcrLock::~RecursiveScopedMcrLock()+008293
    [ 69] 0x00000001004ef902 /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00063746 mvm::detail::RecursiveScopedMcrLock::~RecursiveScopedMcrLock()+010098
    [ 70] 0x00000001004efc3c /Applications/MATLAB_R2010b.app/bin/maci64/libmwmcr.dylib+00064572 mvm::detail::RecursiveScopedMcrLock::~RecursiveScopedMcrLock()+010924
    [ 71] 0x00007fff8ee437a2 /usr/lib/system/libsystem_c.dylib+00083874 _pthread_start+000327
    [ 72] 0x00007fff8ee301e1 /usr/lib/system/libsystem_c.dylib+00004577 thread_start+000013

  20. Dylan Muir

    Hi,

    I have a question about how to control which version of numpy is used by matpy. I think I have only numpy version 1.7.1, but from within the python shell accessed from py_shell, numpy.__version__ is always 1.6.1. I can’t see if or how any version of numpy is specified during compilation or on execution. Am I missing something?

    D.

  21. Fred

    Hi,

    I’m quite new to python. I used only Matlab before but now it is interesting for me to use SciKit from Matlab. If I use the script directly in python it works quite fine but if I try to use it with MatPy, I get the following error:

    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/forest.py”, line 292, in fit
    for i in xrange(n_jobs))
    File “/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py”, line 451, in __call__
    self._pool = multiprocessing.Pool(n_jobs)
    File “/usr/lib/python2.7/multiprocessing/__init__.py”, line 232, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild)
    File “/usr/lib/python2.7/multiprocessing/pool.py”, line 136, in __init__
    self._repopulate_pool()
    File “/usr/lib/python2.7/multiprocessing/pool.py”, line 199, in _repopulate_pool
    w.start()
    File “/usr/lib/python2.7/multiprocessing/process.py”, line 130, in start
    self._popen = Popen(self)
    File “/usr/lib/python2.7/multiprocessing/forking.py”, line 116, in __init__
    sys.stdout.flush()
    AttributeError: ‘module’ object has no attribute ‘flush’

    I tried to import stdout but it doesn’t fit the problem. Any suggestions?

    Regards,

    Fred

    • admin

      What’s on that line in the script? It seems that whatever your object is, Python sees it as a module. I don’t know any module containing flush function.

      • Fred

        I’ll check it but if I run the script without multithreading it works fine. I think it is an issue if you try to use multithreading in python from matlab.

        • admin

          Could be. In general, the most frequent problem I’ve observed with Python in MATLAB is that there are some conflicting DLLs in its memory space which prevent certain Python modules from loading.

  22. Simo

    We are running multifunction python code, something like an hour per run, from light matlab system calls. The python code creates results which have been saved in a file. Now we need to start reading the python result back to matlab while the matlab is running because because we start fitting parameters in the matlab, and then run the next set of python calls. Do you recommend matpy for such work where matlab has to wait long times for the result, or would it be simpler to just read a folder from matlab from time to time. What do you suggest?

  23. Edward

    I am very new to working with python, and know nothing about C language. I was able to successfully compile the mex file, but when I try to call the mex functions, it gives me the following error:

    Invalid MEX-file
    ‘/home/dcui/pkg64/matlabpackages/matpy/py.mexa64’:
    libpython2.7.so.1.0: cannot open
    shared object file: No such file or
    directory

    I have tried to copy the mentioned libraries to the same folder as the compiled mex. I have also did the LD_PRELOAD, uncommenting the dlfunc and dlopen lines when compiling. My Python is 2.7, and my MATLAB is 2012b. My OS is Ubuntu 12.04 LTS.

    Thanks in advance.

      • Edward

        Although adding LD_LIBRARY_PATH (with also PYTHONHOME) allows me to start MATLAB and compile the mex, but when I run the py_shell, MATLAB crashes, with the following error in the terminal:

        ImportError: No module named site

        Let me explain my situation a little bit better. I am compiling using the Anaconda python distribution, and my compile command in MATLAB is the following:

        mex py.cpp -lpython2.7 -L/hsgs/projects/jhyoon1/pkg64/pythonpackages/anaconda/lib/ -I/hsgs/projects/jhyoon1/pkg64/pythonpackages/anaconda/include/python2.7/ -ldl

        I know this might be due to my lack of understanding of python and computers in general, but could you help me troubleshoot this?

        Thanks.

        • admin

          There’s not much mystery behind this. The native binary parts have to see one another (thanks to tricks with LD_PRELOAD and LD_LIBRARY_PATH) and then you have also to point the binaries to the Pythonic part of things (modules) which on my PC (Ubuntu) reside in /usr/lib/python2.7. Just look for site.py. Normally you can use the PYTHONPATH variable for pointing to that path. On the other hand when using regular Python (not Anaconda) and pythonxy it seemed to know automatically where to look for modules but this could’ve been for multiple reasons (e.g. hardcoded paths, looking in the directories where binaries were, etc.). I haven’t tested with Anaconda.

          For sure your current situation is better because the mex already creates Python environment. The import error comes from Python now.

  24. Andre

    Now py_import works with cell2mat (List of python vs Cell of Matlab).
    My code:

    function py_import(varargin)
    for i=1:numel(varargin)
    tmp = py(‘get’, varargin{i});
    % List of list (python)
    if iscell(tmp)
    if iscell(tmp{1})
    if iscell(tmp{1}{1}) % List of list of list
    disp(‘cell of cell of cell not implemented’)
    elseif size(tmp{1}(:),1)<=2 % 2 columns only
    disp('cell of cell, two columns')
    tmp = cell2mat(horzcat(tmp{:})')+1;
    else
    disp('cell of cell, sparse matrix') % Adjacency
    N = numel(tmp);
    A = zeros(N);
    for j=1:N
    s = numel(tmp{j});
    for k=1:s
    t = tmp{j}{k}+1;
    A(j,t) = 1;
    A(t,j) = 1;
    end
    end
    tmp = A; % Matrix
    end
    else
    disp('cell, vector')
    tmp = cell2mat(tmp);
    end
    else
    disp('scalar') % Scalar
    end
    assignin('base', varargin{i}, tmp);
    end
    end

  25. remik

    Thanks. Worked for me on 64bit linux and matlab79 and python2.7.3.
    I had to:

    1) #include dlfcn.h
    2) call dlopen(“libpython2.7.so” …)
    3) link with -ldl


    # Makefile
    INCS = -I/usr/local/x64/python/2.7.3/include/python2.7/
    LIBS = -L/usr/local/x64/python/2.7.3/lib/ -lpython2.7 -ldl
    MEX = /usr/local/matlab.7.9/bin/mex

    all:
    $(MEX) py.cpp $(INCS) $(LIBS)

  26. Jameson

    I am trying it on windows 7 with matlab 8.3. I compiled and started a shell sucessfully. However, when I try to py_export I get an access violation and matlab crashes.

    Any help?

    -Jameson

  27. kazino59

    игровые автоматы онлайн обезьянки, советские игровые автоматы снайпер.

  28. Sam

    Somewhat new to using mex in general. I get this error upon entering mex py.cpp
    >> mex py.cpp
    Building with ‘Xcode Clang++’.
    Error using mex
    /Users/samuel—-/Downloads/matpy1/py.cpp:27:10: fatal error: ‘Python.h’
    file not found
    #include
    ^
    1 error generated.

    Running python 3.4. changing to #include didn’t do anything. Any thoughts on what to do?

  29. Aditya

    Hi,
    I have been trying to get this working with R2013b and python 2.7 on ubuntu 14.04. I can compile it using

    mex py.cpp -g -v -I/usr/include/python2.7 -L/usr/lib/python2.7/config-x86_64-linux-gnu/ -lpython2.7 -ldl

    But when I run py_shell in matlab, I get the below…
    >> py_shell
    py> 5+5
    Traceback (most recent call last):
    File “/usr/lib/python2.7/dist-packages/numpy/__init__.py”, line 153, in
    from . import add_newdocs
    File “/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py”, line 13, in
    from numpy.lib import add_newdoc
    File “/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py”, line 8, in
    from .type_check import *
    File “/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py”, line 11, in
    import numpy.core.numeric as _nx
    File “/usr/lib/python2.7/dist-packages/numpy/core/__init__.py”, line 6, in
    from . import multiarray
    ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyExc_SystemError
    Error in sys.excepthook:
    Traceback (most recent call last):
    File “/usr/lib/python2.7/dist-packages/apport_python_hook.py”, line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
    File “/usr/lib/python2.7/dist-packages/apport/__init__.py”, line 5, in
    from apport.report import Report
    File “/usr/lib/python2.7/dist-packages/apport/report.py”, line 16, in
    from xml.parsers.expat import ExpatError
    File “/usr/lib/python2.7/xml/parsers/expat.py”, line 4, in
    from pyexpat import *
    ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so: undefined symbol: _Py_ZeroStruct

    Original exception was:
    Traceback (most recent call last):
    File “/usr/lib/python2.7/dist-packages/numpy/__init__.py”, line 153, in
    from . import add_newdocs
    File “/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py”, line 13, in
    from numpy.lib import add_newdoc
    File “/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py”, line 8, in
    from .type_check import *
    File “/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py”, line 11, in
    import numpy.core.numeric as _nx
    File “/usr/lib/python2.7/dist-packages/numpy/core/__init__.py”, line 6, in
    from . import multiarray
    ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyExc_SystemError

    =============================
    The entire compilation log
    =============================
    mex py.cpp -g -v -I/usr/include/python2.7 -L/usr/lib/python2.7/config-x86_64-linux-gnu/ -lpython2.7 -ldl

    **************************************************************************
    Warning: Neither -compatibleArrayDims nor -largeArrayDims is selected.
    Using -compatibleArrayDims. In the future, MATLAB will require
    the use of -largeArrayDims and remove the -compatibleArrayDims
    option. For more information, see:
    http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
    **************************************************************************

    -> mexopts.sh sourced from directory (DIR = $PREF_DIR)
    FILE = /home/userName/.matlab/R2013b/mexopts.sh
    —————————————————————-
    -> MATLAB = /home/userName/MATLAB
    -> CC = gcc
    -> CC flags:
    CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
    CDEBUGFLAGS = -g
    COPTIMFLAGS = -O -DNDEBUG
    CLIBS = -Wl,-rpath-link,/home/userName/MATLAB/bin/glnxa64 -L/home/userName/MATLAB/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
    arguments = -DMX_COMPAT_32
    -> CXX = g++
    -> CXX flags:
    CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
    CXXDEBUGFLAGS = -g
    CXXOPTIMFLAGS = -O -DNDEBUG
    CXXLIBS = -Wl,-rpath-link,/home/userName/MATLAB/bin/glnxa64 -L/home/userName/MATLAB/bin/glnxa64 -lmx -lmex -lmat -lm
    arguments = -DMX_COMPAT_32
    -> FC = gfortran
    -> FC flags:
    FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
    FDEBUGFLAGS = -g
    FOPTIMFLAGS = -O
    FLIBS = -Wl,-rpath-link,/home/userName/MATLAB/bin/glnxa64 -L/home/userName/MATLAB/bin/glnxa64 -lmx -lmex -lmat -lm
    arguments = -DMX_COMPAT_32
    -> LD = g++
    -> Link flags:
    LDFLAGS = -pthread -shared -Wl,–version-script,/home/userName/MATLAB/extern/lib/glnxa64/mexFunction.map -Wl,–no-undefined -Xlinker -export-dynamic
    LDDEBUGFLAGS = -g
    LDOPTIMFLAGS = -O
    LDEXTENSION = .mexa64
    arguments = -L/usr/lib/python2.7/config-x86_64-linux-gnu/ -lpython2.7 -ldl
    -> LDCXX =
    -> Link flags:
    LDCXXFLAGS =
    LDCXXDEBUGFLAGS =
    LDCXXOPTIMFLAGS =
    LDCXXEXTENSION =
    arguments = -L/usr/lib/python2.7/config-x86_64-linux-gnu/ -lpython2.7 -ldl
    —————————————————————-

    Warning: You are using gcc version “4.8.2”. The version
    currently supported with MEX is “4.7.x”.
    For a list of currently supported compilers see:
    http://www.mathworks.com/support/compilers/current_release/

    -> g++ -c -I/usr/include/python2.7 -I/home/userName/MATLAB/extern/include -I/home/userName/MATLAB/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32 -g “py.cpp”

    In file included from /usr/include/python2.7/Python.h:58:0,
    from py.cpp:27:
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/stringobject.h:65:5: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
    ^
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/stringobject.h:67:5: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
    ^
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/pyerrors.h:196:25: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 2, 3)));
    ^
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/pyerrors.h:321:25: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
    ^
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/pyerrors.h:323:25: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
    ^
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/sysmodule.h:18:4: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
    ^
    /usr/include/python2.7/pyport.h:901:44: warning: ‘mexPrintf’ is an unrecognized format function type [-Wformat=]
    #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
    ^
    /usr/include/python2.7/sysmodule.h:20:4: note: in expansion of macro ‘Py_GCC_ATTRIBUTE’
    Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
    ^
    py.cpp: In function ‘void initaview()’:
    py.cpp:58:32: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    PySys_SetObject(“stdout”, m);
    ^
    py.cpp:59:32: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    PySys_SetObject(“stderr”, m);
    ^
    py.cpp: In function ‘mxArray* py2mat(PyObject*)’:
    py.cpp:229:64: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    PyObject *list = PyObject_CallMethod(reshaped, “tolist”, NULL);
    ^
    -> g++ -g -pthread -shared -Wl,–version-script,/home/userName/MATLAB/extern/lib/glnxa64/mexFunction.map -Wl,–no-undefined -Xlinker -export-dynamic -o “py.mexa64” py.o -L/usr/lib/python2.7/config-x86_64-linux-gnu/ -lpython2.7 -ldl -Wl,-rpath-link,/home/userName/MATLAB/bin/glnxa64 -L/home/userName/MATLAB/bin/glnxa64 -lmx -lmex -lmat -lm

    • Aditya

      Got it working. Below are the steps I followed…
      Please remember to modify the paths as required!!

      1) Uncomment line 371
      dlopen(“libpython2.7.so”, RTLD_LAZY |RTLD_GLOBAL);

      I also modified the python lib version

      2) Compile (mex) in unix shell using the below
      /mex -g -v py.cpp -I/usr/include/python2.7 -L/usr/lib -DLIBPYTHON=/usr/lib/python2.7/config/libpython2.7.so ‘-lpython2.7 -ldl -Xlinker -export-dynamic’ -compatibleArrayDims

      Of course you can do the same in Matlab command window

      3) Export a few path variables and then run Matlab using the LD_PRELOAD trick as mentioned in the above comments

      ############# EXPORT #################################
      export FFTW_VERSION=/usr/lib/libfftw3.so.3
      export BLAS_VERSION=/usr/lib/libblas.so.3gf
      export LAPACK_VERSION=/usr/lib/lapack/liblapack.so.3gf
      export LAPACK_VERBOSITY=1
      ######################################################

      # Run Matlab
      LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/lib/x86_64-linux-gnu/libgcc_s.so.1:/usr/lib/x86_64-linux-gnu/libgfortran.so.3:/usr/lib/libblas/libblas.so.3gf:/usr/lib/liblapack.so.3gf matlab

  30. Michael

    I have just set up my system for matpy, but it still does not seem to work…

    Setup:
    – Windows 7 64bit
    – MATLAB R2014a 64bit
    – WinPython 2.7.9.2 64bit

    in MATLAB:
    mex py.cpp ‘-IC:Program FilesWinPython-64bit-2.7.9.2python-2.7.9.amd64include’ ‘-LC:Program FilesWinPython-64bit-2.7.9.2python-2.7.9.amd64libs’
    Building with ‘Microsoft Visual C++ 2013 Professional’.
    MEX completed successfully.

    Now, if I type

    >> py_shell
    py> print “Hello, World!”
    py>

    nothing happens. How can I check whether my system is operational?

  31. Abe

    Does this package works for python3? I’ve tried to compile using

    mex py.cpp -IC:\Users\python3\include -lpython3.lib

    It keeps showing that a few functions aren’t declared in this scope. For example, Py_InitModule, PyString_FromString, etc. Does it has something to with the dynamic loaded extension?

    Thanks,
    Abe

Leave a Reply

Your email address will not be published. Required fields are marked *