Skip to main content
The Kitchen Archive

Eats Archive › Lists, guides and FAQs

Lists, guides and FAQs

CREATION AND USE OF MEX FILES

1 post · Jun 16, 1994

Drea T.The MathWorks, Inc., Natick, MA 01760Jun 16, 1994
Jason D. wrote:
|> Can somebody please outline the steps required to create a mex file using
|> BORLAND C++ for Windows.  I understand this can only be achieved with
|> a 32-bit C compiler, but that's all I understand at the moment.
|>
|> Also, can somebody point me towards an example of USING mexes in
|> MATLAB?
|>
|> Thanks
|>
|> Jason Daly
|>
|> e-mail : [email removed]
|>

There is a tecnote that provides an example of a C++ MEX-file. I'll include it below. The tecnotes (answers to some commonly asked questions) are available via anonymous ftp from ftp.mathworks.com in /pub/tech-support/tech-notes or via WWW using the URL http://www.mathworks.com/Technical.html.

MATLAB Technical Note

Creating C++ MEX-files
----------------------

/*  The following example illustrates how to use C++ code with your
    C language MEX-file for MATLAB v4.x.  It makes use of member
    functions, constructors, destructors, and the iostream.

    The file is called example.cxx.  The routine simply defines
    a class, constructs a simple object, and displays the initial
    values of the internal variables.  It then sets the data members
    of the object based on the input given to the MEX-file and displays
    the changed values.

    The calling syntax is:    example( num1, num2 )

    The mexFunction definition, math.h, and mex.h are wrapped with extern
    "C" so that it has "C linkage" for the benefit of MATLAB.  Within the
    body of the mexFunction definition, full C++ syntax is allowed.

    At runtime you have to tell the  executable (in this case the output
    of cmex) where to find the libraries that resolve the C++ symbols.
    It needs to know this in order to go look for them at load time.
    Compile your MEX-file like this:

         g++ -c -I<directory containing mex.h> example.cxx
         cmex example.o -L/hub/lib/sun4 -lg++ -lgcc

    Jeffery Faneuff     April 1, 1994
    Copyright (c) 1994 The Mathworks, Inc.

*/

#include <iostream.h>

extern "C" {
#include <math.h>
#include "mex.h"
}

/****************************/
class MyData {

public:
        void display;
        void set_data(double v1, double v2);
        MyData(double v1 = 0, double v2 = 0);
        ~MyData { }
private:
        double val1, val2;
};

MyData::MyData(double v1, double v2) { val1 = v1; val2 = v2; }

void MyData::display {
        cout << "Value1 = " << val1 << "\n";
        cout << "Value2 = " << val2 << "\n\n";
 }

void MyData::set_data(double v1, double v2) { val1 = v1; val2 = v2; }

/*********************/

static
  void example(    double     num1, double num2 )
{
        cout << "\nThe initialized data in object:\n";
        MyData *d = new MyData; // Create a  MyData object
        d->display;           // It should be initialized to zeros
        d->set_data(num1,num2); // Set data members to incoming values
        cout << "After setting the object's data to your input:\n";
        d->display;           // Make sure the set_data worked
        delete(d);
        return;
}

extern "C" {
  void mexFunction(
                   int          nlhs,
                   Matrix       *plhs[],
                   int          nrhs,
                   Matrix       *prhs[]
                   )
    {
     double      *vin1, *vin2;

      /* Check for proper number of arguments */

      if (nrhs != 2) {
        mexErrMsgTxt("EXAMPLE requires two input arguments.");
      } else if (nlhs >= 1) {
        mexErrMsgTxt("EXAMPLE requires no output argument.");
      }

      vin1 = mxGetPr(prhs[0]);
      vin2 = mxGetPr(prhs[1]);

      example(*vin1, *vin2);
      return;
    }
}                          /* end of extern "C" */
Share this page