How can I call a MATLAB created shared library from a high level programming language?

5 views (last 30 days)
I have created a MATLAB shared library by compiling a MATLAB file. I would like to be able to call my library from a high level programming language.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Jan 2013
A high level view of you project might look something like a set of nested boxes. In the innermost box is the MATLAB MCR, it understands MATLAB code. When you generate a library from your MATLAB files you are creating a box or 'wrapper library' around the MCR that provides a interface to the MCR from some other language.
If you are using a lower level language like C or C++ that allows you to use mxArrays(for C) and mwArrays(for C++), you can compile your programs with the ability to load and then issue commands directly to the second box (MATLAB shared library). If this is the case you are finished, you can run your C/C++ program that will perform whatever operations you need and return the results.
If you are using a higher level language, one that does not alow you to use C/C++ code, you need to go through the intermediate step of creating a third box(wrapper library). This library will facilitate conversion from native data types(int, double, char etc.) to mxArrays and mwArrays. It will also call the MATLAB generated wrapper library functions, which in turn can interface with the MCR.
Here is an overview of the files in the 'Resolution Documents' as well as the files that are created during compilation and the roles they play in the process outlined above. The attached files are commented so that you can get a better idea of what is going on.
The first box. The MCR:
This is the MATLAB runtime that will do all of the processing of the commands that come from the second box.
The second box. MATLAB generated shared library files:
foo1.m, Original MATLAB file code.
foo2.m, Original MATLAB file code.
libfoo1.lib, libfoo2.lib, libfoo1.dll, libfoo2.dll, libfoo1.h, libfoo2.h, libfoo1.ctf and libfoo2.ctf, These are the compiled files that are generated by MATLAB and make up the actual first wrapper (second box) around the MCR. To generate C++ libraries from the MATLAB files use the following commands:
mcc -B cpplib:libfoo1 foo1.m
mcc -B cpplib:libfoo2 foo2.m
The third box. C++ wrapper library files:
w32lib/w32lib.cpp, w32lib/w32lib.h, C++ code that defines the wrapper library between the higher level program and the and the MATLAB generated wrapper library.
Compiling this library will create w32lib.dll, w32lib.lib that make up the C++ wrapper.
High level program:
program.cpp is a C++ file that makes calls through the C++ wrapper file (w32lib.dll) to the compiled MATLAB file shared library. These commands are the analog of the commands you would issue from whatever high level language you are using.
So, to implement this on any system you will need to figure out what you would like to do in the high level language and how you want to use MATLAB to help. You need to identify the types of arguments you can send from the high level language and create a wrapper file in C or C++ to convert those arguments to inputs that your MATLAB generated library can work with. Finally, you will need to deploy your wrapper library, the MATLAB library, and the MCR on the machine you want to use.
There is a limitation to this method. Once a MATLAB compiled shared library has been loaded in a process, it cannot be fully unloaded. This prevents reloading the same library or loading any other MATLAB compiled shared library in the same process using the wrapper library approach. One strategy is to use the wrapper library to load all the MATLAB compiled shared library needed during initialization. Another strategy is to put related MATLAB functions in the same shared library and load it once using the wrapper library.

More Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!