core/matlab/examples/mex/mexcpp.cpp

00001 /*
00002  * The following example illustrates how to use C++ code with
00003  * your C language MEX-file for MATLAB 5 for UNIX or Microsoft Visual.
00004  * It makes use of member functions, constructors, destructors, and the
00005  * iostream.
00006  *
00007  * The routine simply defines a class, constructs a simple object,
00008  * and displays the initial values of the internal variables.  It
00009  * then sets the data members of the object based on the input given
00010  * to the MEX-file and displays the changed values.
00011  *
00012  * The file is called mexcpp.cpp.  The extension was chosen rather
00013  * arbitrarily and as being unambiguous and generally recognized by
00014  * C++ compilers.  The mex script should recognize any other common
00015  * C++ source file extension, such as .C, .cc, or .cxx.
00016  *
00017  * FOR UNIX:
00018  * In order to compile this example, invoke mex as:
00019  *
00020  *   mex mexcpp.cpp
00021  *
00022  * FOR NT:
00023  * Be sure to set your environment for MSVC++ 5.0.  In order to do this,
00024  * run 'mex -setup' at the MATLAB command prompt.  Then invoke mex as:
00025  *
00026  *   mex mexcpp.cpp
00027  *
00028  *
00029  * The calling syntax is:    mexcpp( num1, num2 )
00030  *
00031  * If you run your MEX-file in MATLAB and you do not receive the expected
00032  * output make sure that you have a C++ flush() function call in your
00033  * program.  Also, for NT, cout will not display, since there is no shell
00034  * to display to.  Therefore, you must use mexPrintf.
00035  *
00036  * Copyright 1984-2006 The MathWorks, Inc.
00037  * All Rights Reserved.
00038  * $Revision: 1.1 $
00039  */
00040 #include <iostream>
00041 #include <math.h>
00042 #include "mex.h"
00043 
00044 using namespace std;
00045 
00046 extern void _main();
00047 
00048 /****************************/
00049 class MyData {
00050 
00051 public:
00052   void display();
00053   void set_data(double v1, double v2);
00054   MyData(double v1 = 0, double v2 = 0);
00055   ~MyData() { }
00056 private:
00057   double val1, val2;
00058 };
00059 
00060 MyData::MyData(double v1, double v2)
00061 {
00062   val1 = v1;
00063   val2 = v2;
00064 }
00065 
00066 void MyData::display()
00067 {
00068 #ifdef _WIN32
00069     mexPrintf("Value1 = %g\n", val1);
00070     mexPrintf("Value2 = %g\n\n", val2);
00071 #else
00072   cout << "Value1 = " << val1 << "\n";
00073   cout << "Value2 = " << val2 << "\n\n";
00074 #endif
00075 }
00076 
00077 void MyData::set_data(double v1, double v2) { val1 = v1; val2 = v2; }
00078 
00079 /*********************/
00080 
00081 static
00082 void mexcpp(
00083         double num1,
00084         double num2
00085         )
00086 {
00087 #ifdef _WIN32
00088     mexPrintf("\nThe initialized data in object:\n");
00089 #else
00090   cout << "\nThe initialized data in object:\n";
00091 #endif
00092   MyData *d = new MyData; // Create a  MyData object
00093   d->display();           // It should be initialized to
00094                           // zeros
00095   d->set_data(num1,num2); // Set data members to incoming
00096                           // values
00097 #ifdef _WIN32
00098   mexPrintf("After setting the object's data to your input:\n");
00099 #else
00100   cout << "After setting the object's data to your input:\n";
00101 #endif
00102   d->display();           // Make sure the set_data() worked
00103   delete(d);
00104   flush(cout);
00105   return;
00106 }
00107 
00108 void mexFunction(
00109          int          nlhs,
00110          mxArray      *[],
00111          int          nrhs,
00112          const mxArray *prhs[]
00113          )
00114 {
00115   double      *vin1, *vin2;
00116 
00117   /* Check for proper number of arguments */
00118 
00119   if (nrhs != 2) {
00120     mexErrMsgTxt("MEXCPP requires two input arguments.");
00121   } else if (nlhs >= 1) {
00122     mexErrMsgTxt("MEXCPP requires no output argument.");
00123   }
00124 
00125   vin1 = (double *) mxGetPr(prhs[0]);
00126   vin2 = (double *) mxGetPr(prhs[1]);
00127 
00128   mexcpp(*vin1, *vin2);
00129   return;
00130 }

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy