Note that the following information is valid for MATLAB releases R2008a - R2013b. For newer releases see:
MATLAB Compiler 4.8 (R2008a) generated DLLs do not have the ability to enable functions calls without first initializing the MCR and the library. Addtionally, they deal primarily with mxArray data types.
To work around this, the MATLAB DLL can be wrapped by an intermediate level of code which does the following:
1. Initialize the MCR and load the MATLAB DLL (the first time it is called).
2. Convert data formats of the driver into MATLAB data (mxArray).
3. Call the MATLAB functions from the MATLAB DLL.
4. Convert MATLAB data back into data type formats that the driver can understand.
The attached file shows an example using C code.
This example was tested using MS Visual C++ 2005 Express Edition and the LCC compiler.
Level 1 : MATLAB code
----------------------
level1.m contains the MATLAB code which implements the functionality required from the MATLAB DLL. level1.m contains a MATLAB function which takes a 2D matrix and a string. The function displays the string using a dialogue box and returns the input matrix multiplied by 2.
A DLL is compiled from this MATLAB code using the following command:
NOTE: Ensure that a level1.dll from a previous run does not exist on the path before executing the following.
mcc -v -B csharedlib:level1 level1.m
This step creates level1.dll, level1.lib and level1.h among others.
Level 2 : Wrapper code
-----------------------------
The wrapper code level2.c handles the four tasks mentioned above. This is compiled to create level2.dll using the following command:
mbuild -v level2.c level1.lib level2.exports
level2.exports is a plain text file mentioning the functions names in level2.c which need to be exported (visible) in the generated level2.dll file. Please note that level2.dll is dependent on level1.dll
Level 3 : Driver code
--------------------------
This code is indicative of the final driver code. No initialization steps are required and the functions can be called with C data types. This driver code, level3.c, is compiled using the following command:
Note that the final execution of level3.exe requires level2.dll and level1.dll to be available in the same directory as this executable or somewhere else on the system path.
Also see the Related Solution below for more information.
Best Answer