Using Matlab Dlls RRS feed

  • Question

  • Hi everybody !

    I am a Matlab Programmer. I create some Dlls from my .m files.
    For the first test, my .m file contains only one function with one argument. the entire .m file is as follows:

    function y=test(x)

    Then, by using the following command , I created the Dll and some other files:

    mcc -B csharedlib:testlib test.m


    These files listed below:


    Now I want simply write a visaul c++ 2008 windows forms application that use this function.

    There are many topics in forum about Dlls, but I could not use those helps because I very new in VC++.

    If anyone can help me with detailed explanation I'll be very very grateful.

    • Changed type Engineer Mahdi Monday, November 23, 2009 7:06 PM I have some questions
    Friday, November 13, 2009 6:23 PM

All replies

  • Look at this article at CodeProject.
    Saturday, November 14, 2009 10:53 AM
  • Thanks Nikita
    but this link is for old version of visual c++ and don't match my problem.
    Are there any other help?
    Monday, November 16, 2009 12:49 AM
  • Code that is written in the article should work on any version of VS.
    Monday, November 16, 2009 8:16 AM
  • Thanks but I very new to VC++ and want a very very detailed, step by step help.
    Monday, November 16, 2009 10:23 AM
  • CodeProject article describes in detail all the steps. You need to create new project that you want and put code from the article in to your project cpp file.
    Monday, November 16, 2009 11:19 AM
  • Thanks
    I test the above link and get errors.
    I don't know what's the Problem.
    Monday, November 23, 2009 4:36 AM
  • Put yourself in our shoes. How can we help unless you tell us what errors you encountered?
    Monday, November 23, 2009 5:11 AM
  • Ok, I test the above link again and tell you the errors.
    Be patient.
    Monday, November 23, 2009 7:10 AM
  • In the above link, "Matlab.h" file has been used, I don't find this file.
    Monday, November 23, 2009 7:35 AM
  • Matlab.h is situated in <matlab dir>\extern\include. You should go Tools menu > Options > Projects and Solutions > VC++ Directories, select Show directories for: Include files and add dir to the list.
    Additional info in this article that I found on mathworks site.
    Monday, November 23, 2009 4:29 PM
  • I did the instructions.

    /* Call the implementation function */
    y_ptr = mlfTestdll(x_ptr);

    What's the number of arguments of above function?

    My cpp file looks:

    #include "testdlllib.h"
    //#include "matlab.h"
    #include <stdio.h>
    void main(void)
    mxArray *x_ptr;
    mxArray *y_ptr;
    double *y;
    double ret;
    /* Create an mxArray to input into mlfFoo */
    x_ptr = mxCreateDoubleScalar(1);
    /* Call the library initialization function */
    /* Call the implementation function */
    y_ptr = mlfTestdll(x_ptr);
    /* Call the library termination function */
    /* The return value from mlfFoo is an mxArray so we must extract the data from it */
    y = mxGetPr(y_ptr);
    ret = *y;
    /* Print a double precision number*/
    printf("The output of foo is %f\n",ret);
    When I build the program, One error occurs:

    error C2660: 'mlfTestdll' : function does not take 1 arguments
     Please Help Me.
    Monday, November 23, 2009 5:38 PM
  • mlfTestdll is function that you are using in Matlab. I don't know how many params are you using in it. Check it.
    Monday, November 23, 2009 5:53 PM
  • In the Matlab We use testdll function, but according to the, we must use mlfTestdll function.
    My function In matlab is using 1 argument.
    Monday, November 23, 2009 6:00 PM
  • In matlab you have testdll function and mcc creates testlib.h and declares function as mlfTestdll in it. As I understood.
    Monday, November 23, 2009 6:10 PM
  • But in the matlab my function has one argument.
    Why In VC++ It requires 3 arguments?
    And How I give these arguments to function.
    Monday, November 23, 2009 6:21 PM
  • I don't know. It is on matlab side.

    By the way, why #include "matlab.h" line is commented in your code. I told you how to add include path for this header in VC in one of the previous posts.
    Maybe it is the cause of your problem.
    Monday, November 23, 2009 6:43 PM
  • And change topic from discussion to question, because you ask questions.
    Monday, November 23, 2009 6:46 PM
  • So, What's the solution? Please contribute
    Tuesday, November 24, 2009 10:29 AM
  • Return #include "matlab.h" line in your project.
    Tuesday, November 24, 2009 10:47 AM
  • I 've the same problem with my function parameters !

    I already added #include "matlab.h"


    but the problem is with parameters as I built a DLL for bwtraceboundary MATLAB function then mlfBwtraceboundary has different parameters from bwtraceboundary I found it in the mcc file mlfBwtraceboundary(int nargout, mxArray** B, mxArray* varargin);

    How can I convert int* to mxArray** ??

    and I amn't sure of those parameters as in matlab function or not

    I used to call it in matlab by 


    BW = im2bw(I);
    dim = size(BW);
    col = round(dim(2)/2)-90;
    row = min(find(BW(:,col)));
    boundary = bwtraceboundary(BW,[row, col],'S',8);
    hold on;
    BW_filled = imfill(BW,'holes');
    boundaries = bwboundaries(BW_filled);
    for k=1:10
       b = boundaries{k};

    now I wrote 

    mxArray* varargin={height, width};
    	mxArray** B=labelsOut;int nargout;
    	boundary = mlfBwtraceboundary(8,B,varargin);
    	printf("The output of bwtraceboundary is %f\n",boundary);

    any one can help me in converting the parameters and passing them ?

    Thanks in advance

    Tuesday, May 4, 2010 12:04 PM
  • matlab.h and libmatlb.h are not present in 2011b version. 


    How can we modify this the code present on link below to 2011b version


    Wednesday, January 18, 2012 12:13 PM