How to Install mlabwrap on Windows

mlabwrap Extension for Python

It’s totally OK if you’ve never heard of mlabwrap, it is a Python extension which can build a nice connection between Python and MATLAB, lets you call MATLAB functions and M-files right in Python without further configuration.

And for those of you who comes from google, you are now using speed=10m/s to jump over this damn intro of mlabwrap.


So, Let’s Start

mlabwrap ships the following files when you’ve download it from sourceforge. The hard part is, you have to tune some of them to make sure it’s installable on your computer. The author (Alexander Schmolck) has never tested it on Windows, so it’s very hard to configure and install. But if you’re good at Python and C++, it’s another thing.

mlabwrap files before installation

mlabwrap Files Before Installation

For the installation of mlabwrap you’ll need:

Make sure you install all of this before start with mlabwrap!


Some Words for MATLAB

Basically any version of MATLAB should do, but the author has point out the MATLAB 2008a can have some problem with this extension, so take MATLAB 2007, 2008b, 2009 or 2010 if you want to avoid some trouble. Besides, I prefer to install the x32 version of MATLAB and the compiler, the x64 version always gives out some not-so-good suprises.

After you have installed C compiler, you should make a short test in MATLAB base on this instruction from MathWorks to look if it’s functioning well. Otherwise you may get some strange compilation error at installing mlabwrap.


Tuning the mlabwrap

Now let’s quick tune the mlabwrap files.

1. mlabraw.cpp

mlabraw.cpp

The original shipped mlabraw.cpp file couldn’t pass the Visual C++ compiler on my computer, which I can’t explain. But you could change these two places to bring it back to life if you also run Visual C++ compiler.

// In line 645 change the code from
if (not ok) {
// To
if (! ok) {
 
 
// In line 768 change the code from
} else if (mxIsDouble(lArray) and not mxIsSparse(lArray)) {
//To
} else if (mxIsDouble(lArray) && ! mxIsSparse(lArray)) {

Or you can download this file here:


2. setup.py

setup.cpp

What this script basically does is to find out the libraries and included files for MATLAB runtime, setting the C++ compiler information for the mlabwrap installation, when all these are done, it starts the compilation and installation. Several places need to be changed in this file, pay attention to the WINDOWS identifier in the script will make it much more understandable.

At the first part, you need to manually set the MATLAB and C++ compiler paths, here are the settings for me:

MATLAB_COMMAND = 'matlab'
MATLAB_VERSION = 7.3
MATLAB_DIR= 'D:/Program Files (x86)/MATLAB/R2009b'
PLATFORM_DIR = None
EXTRA_COMPILE_ARGS = None
MATLAB_LIBRARIES = None
USE_NUMERIC = None
PYTHON_INCLUDE_DIR = None
SUPPORT_MODULES = ["awmstools"]
VC_DIR = 'D:/Program Files (x86)/Microsoft Visual Studio 9.0/VC'

After that, change the following codes, pay attention to the VC path, adjust it to meet your setting:

# In line 134 change the code from
PLATFORM_DIR = PLATFORM_DIR or queried_platform_dir
# To
PLATFORM_DIR = 'win32/microsoft'
 
 
# In line 162 change the code from
MATLAB_INCLUDE_DIRS += [VC_DIR + "/include", VC_DIR + "/PlatformSDK/include"]
# To
MATLAB_INCLUDE_DIRS += [VC_DIR + "/include", "C:/Program Files/Microsoft SDKs/Windows/v7.0/Include"]
 
 
# In line 185 change the code from
runtime_library_dirs=MATLAB_LIBRARY_DIRS,
# To (Yes, comment it!)
#runtime_library_dirs=MATLAB_LIBRARY_DIRS,

Or you can save your time and download this file here:


And That’s All!

Now start a command window and go to your mlabwrap folder, type in the following code, mlabwrap is now installed!

python setup.py install

I’ll write another post to give you a short intro of how to bring mlabwrap to work. Feel free to leave me comments here if you still have trouble installing mlabwrap.

Related posts:

  1. How To Send E-Mail From MATLAB
  2. Exclamation Mark Your MATLAB?
  3. Beginner: Quick Targeting Inside Vector in MATLAB
  4. Better tic & toc in MATLAB
  5. Know the Possible Property Values

Category: MATLAB/Simulink, Software.

12 Responses to “How to Install mlabwrap on Windows”

  • kimi Says:

    你们所有没有人在弄 用Dynamische Programmierung优化Hybridbetriebstrategie的?

  • Lin Li Says:

    我得下周帮你问问,就只有1个人弄Hybrid的,这个Dynamische Programmierung是什么?没听过…

  • Lolo Says:

    Thank you very much for your installation tutorial. Without it, I would not have been able to achieve to compile this great Python module. Just a few complements.

    With Visual C++ 2010 Express, Python 2.6.5 and Matlab 5.3

    1) Follows instructions according to the versions of the above programs
    2) Put
    “vcvarsall = “C:/Program Files/Microsoft Visual Studio 10.0/VC/vcvarsall.bat”
    instead of
    vcvarsall = find_vcvarsall(version)
    in file : msvc9compiler.py of your python distribution (in my case ActivePython 2.6.5)
    3) With Visual Studio command prompt (from Start menu), create libmx.lib and libeng.lib, from the corresponding .def and .dll supplied with Matlab 5. You must use the command “lib” (see resources on this matter with Google). Copy these two new libs to C:\Program Files\Microsoft Visual Studio 10.0\VC\lib else the linker won’t be able to find them.
    4) Change Windows path so that it includes C:\MATLABR11\bin.
    5) python setup.py install (I need to do that two times, due to an identified .manifest error on the first attempt. Don’t know why.)
    6) When you type import mlabraw, you may encounter an error. This is because one or more dlls on which mlabraw.pyd depends are not included in your system Path. This is true for those present in C:\MATLABR11\bin and msvcr90.dll. At worse, copy them to C:\apps\Python26\Lib\site-packages (though not recommended).

    After that, it works perfectly.

    It took me almost 1 day and a half figuring out how to compile this module, with either MingW (I gave up due to the missing libmx.lib and libeng.lib, and because errors are less explicit than those provided by MSVC) and MSVC Express 2010. So I hope my experience will help somebody else.

    This sort of trouble is very appaling, potentially preventing people to take benefit from those great tools. Too sad.
    Standardized and rock-solid compilation mechanisms and tools should be provided with any Python distribution to make such process almost transparent.

  • Tymon Says:

    Guys,

    Thank you for sharing your experience. I’m experienced with matlab but new to python. It just happens that I need to use a commercial software that comes with a python interface so mlabwarp would be great to be able to access from python all the optimization code I already have written in matlab.

    I’m using matlab 7.8 (r2009a), python 2.7.1, and MSVC Express 2010 on Windows XP x64 (although matlab and python are 32 bit versions). That’s not an optimal setup (due to other limitations) although everything works great but for mlabwrap.

    I went through the process described above and had to additionally modify vcvarsall as described by Lolo. Unfortunately, I’m unable to go through the compilation. It fails due to a declaration conflict between matlab’s matrix.h and MSVC’s yvals.h on a variable named “chart16_t”. Googling the issue it appears the common solution is not compatible with my limited C++ skills (haven’t done serious C++ in 10 years) as they require deep editing of the C++ code. Has anybody faced this issue and if you did how did you approach it?

  • javier Says:

    Hi,
    Thanx for this tuto.

    I’ve tried pymatlab-0.1.3, pymat-1.1.90, and mlabwrap-1.1.

    (XP Py2.6 r2010b)

    None of then will install, got always some gcc errors.

    Then I found your blog and id the whole stuff, modifing files and install Visual C++ 2010 Express.

    I cross my fingers and…

    doesn’t work…

    the error this time is :
    “unable to find vcvarsall.bat”

    Hum, by the the way, the cmd refuses

    “python setup.py install”
    doesn’t know python…

    It accepts
    “setup.py install”

    is that the pain ??

    Any help VERY welcome !!!

    Thanx for that !

  • Kin Cheung Says:

    Dear Sir,

    I have made the following changes to the setup.py file: MATLAB_VERSION=7.8
    MATLAB_DIR=’C:/Program Files/MATLAB/R2009a Student’
    VC_DIR=’C:/Program Files/Microsoft Visual Studio 10.0
    MATLAB_INCLUDE_DIRS += … C:/Program Files/ Microsoft SDKs/Windows/V7.0A/Include

    I have also copied the new mlabraw.cpp file as in
    http://obasic.net/how-to-install-mlabwrap-on-windows

    Unfortunately it did not work. Here are the error messages when I ran setup.py:

    Traceback (most recent call last):
    File “C:\Python27\Python27A\mlabwrap-1.1\setup.py”, line 201, in
    extra_compile_args=EXTRA_COMPILE_ARGS,
    File “C:\Python27\Python27A\lib\distutils\core.py”, line 140, in setup
    raise SystemExit, gen_usage(dist.script_name) + “\nerror: %s” % msg
    SystemExit: usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] …]
    or: setup.py –help [cmd1 cmd2 ...]
    or: setup.py –help-commands
    or: setup.py cmd –help

    error: no commands supplied
    >>> import mlabwrap

    Traceback (most recent call last):
    File “”, line 1, in
    import mlabwrap
    File “C:\Python27\Python27A\mlabwrap-1.1\mlabwrap.py”, line 188, in
    import mlabraw
    ImportError: No module named mlabraw

    Please help me. Thank you first.

    Kin Cheung

  • Snehal Says:

    Hello
    Thanks for the tutorial..
    I made all the necessary chnages but its still giving me following error.

    C:\Python27\Lib\site-packages\mlabwrap-1.1>python setup.py install

    WINDOWS SPECIFIC ISSUE? Unable to remove c:\users\snehal\appdata\local\temp\tmpe
    rrp7_; please delete it manually
    [Error 32] The process cannot access the file because it is being used by anothe
    r process: ‘c:\\users\\snehal\\appdata\\local\\temp\\tmperrp7_’

    Traceback (most recent call last):
    File “setup.py”, line 136, in
    queried_version, queried_dir, queried_platform_dir = matlab_params(cmd, WIND
    OWS, extra_args)
    File “setup.py”, line 98, in matlab_params
    error = call(cmd, **extra_args)
    File “C:\Python27\lib\subprocess.py”, line 493, in call
    return Popen(*popenargs, **kwargs).wait()
    File “C:\Python27\lib\subprocess.py”, line 679, in __init__
    errread, errwrite)
    File “C:\Python27\lib\subprocess.py”, line 893, in _execute_child
    startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified

  • Jim W Says:

    Much thanks go to you for providing much help that was needed. In addition to your problems, I having problems because of line 225:

    dims = mxGetDimensions(pArray);

    I get this error saying : cannot convert form ‘const mwSize * to ‘const int *’

    I’m new to C++ but is this an easy problem to fix. I’m trying to integrate matlab r2011b with python 32 bit 2.7.2 and running into this brick wall. I’ll keep digging but any help will be much appreciated. Thanks!

  • Jim W Says:

    An update: Change the line in setup.py to

    MATLAB_VERSION = 7.3 and the above error will go away. No to solve all those linker problems. Haven’t figured that one out yet.

  • Heinz K Says:

    Used the files two files from above, changed R2009b to R2011b, copied all include and lib files from matlab path to VC\include respectively VC\lib.

    Got many error messages of type

    ‘mlabraw.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ..:’

    Anyone got an idea? My c++ skills are pretty elementary..

  • rs Says:

    The instructions worked great – thanks. For some others, who are having issues – do not use MS Visual C++ 2010! I had the same problem as many others at first. I then installed MS Visual C++ 2008 and the instructions worked almost perfectly.

    I also had to comment out lines 87 and 88 in setup.py to get the install to work.

  • A large percentage of of what you claim happens to be astonishingly legitimate and that makes me wonder why I hadn’t looked at this with this light previously. This particular article really did switch the light on for me as far as this subject matter goes.

Leave a Reply