Marcel Levy
Marcel Levy

Reputation: 3437

Creating a shared library in MATLAB

A researcher has created a small simulation in MATLAB and we want to make it accessible to others. My plan is to take the simulation, clean up a few things and turn it into a set of functions. Then I plan to compile it into a C library and use SWIG to create a Python wrapper. At that point, I should be able to call the simulation from a small Django application. At least I hope so.

Do I have the right plan? Are there are any serious pitfalls that I'm not aware of at the moment?

Upvotes: 15

Views: 2509

Answers (4)

Dima
Dima

Reputation: 39389

One thing to remember is that the MATLAB compiler does not actually compile the MATLAB code into native machine instructions. It simply wraps it into a stand-alone executable or a library with its own runtime engine that runs it. You would be able to run your code without MATLAB installed, and you would be able to interface it with other languages, but it will still be interpreted MATLAB code, so there would be no speedup.

Matlab Coder, on the other hand, is the thing that can generate C code from Matlab. There are some limitations, though. Not all Matlab functions are supported for code generation, and there are things you cannot do, like change the type of a variable on the fly.

Upvotes: 6

Eli Bendersky
Eli Bendersky

Reputation: 273526

I'd also try ctypes first.

  1. Use the MATLAB compiler to compile the code into C.
  2. Compile the C code into a DLL.
  3. Use ctypes to load and call code from this DLL

The hardest step is probably 1, but if you already know MATLAB and have used the MATLAB compiler, you should not have serious problems with it.

Upvotes: 2

Michal Sznajder
Michal Sznajder

Reputation: 9406

I remember that I was able to wrap a MATLAB simulation into a DLL file and then call it from a Delphi application. It worked really well.

Upvotes: 4

Bartosz Radaczyński
Bartosz Radaczyński

Reputation: 18564

Perhaps try ctypes instead of SWIG. If it has been included as a part of Python 2.5, then it must be good :-)

Upvotes: 1

Related Questions