0

I am aware that dll files created using MATLAB Compiler require MATLAB Compiler Runtime (MCR). So the question is: a) How do I initialize MCR using Python? b) How do I access the functions in the dll file, post MCR initialization?

I am using MATLAB R2012B, MCR v80 and Python 2.7.6 on Windows 7.

Adarsh Chavakula
  • 1,509
  • 19
  • 28
  • Does your question aim at the MATLAB specific issues or on how to call functions in a shared library from python in general? – sebastian May 22 '14 at 06:24
  • The question is aimed particularly at MATLAB specific issues. I am not able to use functions in dll files generated by MATLAB compiler alone. – Adarsh Chavakula May 22 '14 at 06:40

1 Answers1

1

Concerning the MATLAB "features", check this blog entry by Loren:

http://blogs.mathworks.com/loren/2011/02/03/creating-c-shared-libraries-and-dlls/

It shows a pretty complete example of how to create a shared library and how to use it. Quoting example code from that link, to initialize the MCR and the generated library you'll have to call:

// Initialize the MATLAB Compiler Runtime global state
if (!mclInitializeApplication(NULL,0))
{
    std::cerr << "Could not initialize the application properly."
              << std::endl;
    return -1;
}

// Initialize the Vigenere library
if( !libvigenereInitialize() )
{
    std::cerr << "Could not initialize the library properly."
              << std::endl;
    return -1;
}

Where you'll obviously have to replace libvigenere by your library's name.

Now you can call your generated matlab functions just as you would call any C function. And finally, shut down everything:

// Shut down the library and the application global state.
libvigenereTerminate();
mclTerminateApplication();

Concering the connection to python, there are multiple ways, all described e.g. in this question:

Calling C/C++ from python?

Community
  • 1
  • 1
sebastian
  • 9,526
  • 26
  • 54
  • This method involves using C or C++ (C++ in this case) wrapper function to initialize MCR and subsequently load the libraries and use functions in them. However I do not intend to use C or C++ at all. I want to be able to call dlls using python. ctypes.cdll is a way to load C shared dlls but I cannot access the functions in the dlls when they are created using MATLAB compiler. – Adarsh Chavakula May 22 '14 at 09:00
  • Why not? `mclInitializeApplication` should be a plain C function, that you can call e.g. with ctypes from python. All that's done in this example in C++ can be translated to python using `ctypes`. – sebastian May 22 '14 at 17:21