Click here to Skip to main content
15,867,986 members
Articles / Programming Languages / C++
Article

MATLAB Shared Library

Rate me:
Please Sign up or sign in to vote.
4.61/5 (23 votes)
30 Dec 20032 min read 585.9K   4K   54   159
Using MATLAB compiler to build a shared library (DLL) from m-File.

Sample screenshot - Matlab Shared Library

Introduction

Some times it is required that we build a shared library (DLL) from an m-file. M-files are functions that are written in Matlab editor and can be used from Matlab command prompt. In m-files, we employ Matlab built-in functions or toolbox functions to compute something. In my past articles, I showed you some ways to use Matlab engine (vis. API, C++ class or Matlab engine API) for employing Matlab built-in functions, but what about functions that we develop? How can we use them in VC? Is there any interface? This article shows you an idea to employ your own Matlab functions.

Shared Libraries

Shared libraries or DLLs are files that export some functions. We can use exported functions in any language. Here is a brief instruction to build shared libraries from Matlab m-files:

  1. Compile your m-file into a DLL (from Matlab command prompt):
    mcc -t -L C -W lib:mylib -T link:lib -h <M-file> libmmfile.mlib

    The -t option tells the Matlab compiler to translate the m-file to the target language. The -L option specifies the target language, which is chosen to be C. The -W option tells the Matlab compiler to build a wrapper for the library with the name specified by "lib:". The -T option tells the compiler what stage should be reached and for what intentions. Here we link our application together to build a shared library (DLL). Specifying libmmfile.mlib tells Matlab compiler to link against Matlab m-file math routine.

    This step will produce mylib.dll, mylib.lib and mylib.h. For debugging purposes, you can add the -g switch to produce a DLL suitable for debugging in MSVC.

    For example, I wrote my own mean function and saved it as MeanFunction.m:

    function y=MeanFunction(x)
    [m,n]=size(x);
    k=0;
    for i=1:n
        k=k+x(i);
    end
    y=k/n;

    and compiled it with mcc:

    mcc -t -L C -W lib:MeanFunctionLib -T link:lib MeanFunction.m libmmfile.mlib
  2. Create your project in VC. In your main CPP file, include your function header file and add the related library. Here I create a simple console application. Make sure to call initialization and termination routines from your code before and after of calling the m-file function.

    #include "stdafx.h"
    #include "matlab.h"
    #include "MeanFunctionLib.h"
    
    #pragma comment(lib, "libmx.lib")
    #pragma comment(lib, "libmatlb.lib")
    #pragma comment(lib, "libmat.lib")
    #pragma comment(lib, "libmmfile.lib")
    #pragma comment(lib, "MeanFunctionLib.lib")
    
    int main(int argc, char* argv[])
    {
        mxArray* result;
        mxArray* x;
        double myArray[5]={10.2, 3, 6.3, 5.4, 5.9};
        
        x=mxCreateDoubleMatrix(1, 5, mxREAL);
        memcpy(mxGetPr(x), myArray, 5 * sizeof(double));
    
        MeanFunctionLibInitialize();
    
        result=mlfMeanfunction(x);
        
        MeanFunctionLibTerminate();
    
        mlfPrintMatrix(result);
    
        mxDestroyArray(x);
        mxDestroyArray(result);
    
        return 0;
    }
  3. Build your project.

Notice that you must use Matlab C API or Matlab C++ class library to use mxArray or mwArray. For more information, refer to my articles:

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:


You can see list of my articles, by clicking here


Comments and Discussions

 
Questionoutbut of callib is zero. why? Pin
aazam rafiee zade23-Jun-13 3:48
aazam rafiee zade23-Jun-13 3:48 
QuestionHow i can create a library from a group of .m files? Pin
aazam rafiee zade21-Jun-13 8:38
aazam rafiee zade21-Jun-13 8:38 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:54
professionalManoj Kumar Choubey26-Feb-12 19:54 
Generalhaving problems with memcpy Pin
hubertus9510-Jun-11 10:17
hubertus9510-Jun-11 10:17 
Generalproblems using shared library to convert matlab code to c/c++ code Pin
rohith.g16-Jun-10 2:13
rohith.g16-Jun-10 2:13 
Questionhow to convert m files matlab to dll c#.net? Pin
sevda2013-Jun-10 2:06
sevda2013-Jun-10 2:06 
Generalسلام آقای ریاضی Pin
Engineer-m22-Nov-09 20:46
Engineer-m22-Nov-09 20:46 
QuestionAccess violation in libmatlb.dll while enter to function in C++ Builder 6.5/ CodeGear Pin
jb_pl1-Feb-09 10:33
jb_pl1-Feb-09 10:33 
GeneralCompatability of matlab code with c# Pin
awais5510-Sep-08 0:35
awais5510-Sep-08 0:35 
GeneralRandom Numbers Pin
parth.p31-Mar-08 3:11
parth.p31-Mar-08 3:11 
Questiondo we have something like matlab framework? Pin
adnanmn29-Mar-08 2:14
adnanmn29-Mar-08 2:14 
my application is made in vc and it uses matlab generated c/c++ libraries.
but when i run my exe files in a computer that has only .net framework 2, the part of application that has vc code runs fine, but when i click button to open a dialog made in matlab, it says couldnot initialize application.
it runs fine if matlab is installed in the computer.

so do we have something like matlab framework ? or someone have faced and solved similar problem, please do inform,
thank you.
AnswerRe: do we have something like matlab framework? Pin
adnanmn31-Mar-08 0:38
adnanmn31-Mar-08 0:38 
Generalmcc of Matlab 7.0 Pin
Junaid Jameel13-Mar-08 4:21
Junaid Jameel13-Mar-08 4:21 
GeneralRe: mcc of Matlab 7.0 Pin
harbujel27-Jul-08 23:22
harbujel27-Jul-08 23:22 
Generalmatlab .net builder Pin
Paul Stephen10-Feb-08 17:59
Paul Stephen10-Feb-08 17:59 
QuestionI have some problem using m files in C++ , can you help me? Pin
Vahid - Rostami17-Dec-07 16:16
Vahid - Rostami17-Dec-07 16:16 
Questioncan i use matlab inbuild function .... Pin
Chetan Sheladiya21-Jul-07 2:21
professionalChetan Sheladiya21-Jul-07 2:21 
AnswerRe: can i use matlab inbuild function .... Pin
ElvedinHamzagic21-Oct-07 3:37
ElvedinHamzagic21-Oct-07 3:37 
QuestionCalling a dll without header in matlab Pin
ahad4451-Jul-07 21:32
ahad4451-Jul-07 21:32 
GeneralMatlab dll in Borland C++ Builder 6.0 Pin
Elvedin809-Jun-07 5:59
Elvedin809-Jun-07 5:59 
GeneralRe: Matlab dll in Borland C++ Builder 6.0 Pin
ElvedinHamzagic11-Jun-07 7:30
ElvedinHamzagic11-Jun-07 7:30 
GeneralRe: Matlab dll in Borland C++ Builder 6.0 Pin
henriqueamorim8-Oct-07 4:19
henriqueamorim8-Oct-07 4:19 
AnswerRe: Matlab dll in Borland C++ Builder 6.0 Pin
ElvedinHamzagic21-Oct-07 3:19
ElvedinHamzagic21-Oct-07 3:19 
GeneralRe: Matlab dll in Borland C++ Builder 6.0 Pin
ElvedinHamzagic21-Oct-07 4:05
ElvedinHamzagic21-Oct-07 4:05 
Generalmcc Compiler error Pin
kumar Shwetaketu21-May-07 3:54
kumar Shwetaketu21-May-07 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.