How to create a MEX file from a C++ function

Hello, I am currently working on a program that integrates C++, MATLAB, and a geochemical program. I am using the C++ code that they provided to act as a pipe between my main code, written in MATLAB and the geochemical program.
As of now, I have not been able to figure out how to compile the pipe, which I want to solely be a standalone function without a main(). I want it to act like any other MATLAB function. It takes in a few variables and then updates their values as it gets more information from the geochemical program.
So far, I have included:
#include "mex.h" #define printf mexPrintf
and
int main() { cout << "hello world" << endl; return 0; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if( main() ) mexErrMsgTxt("main returned non-zero value");
}
in my code. Obviously the int main doesn't make too much sense, but without it's presence I don't know what I should include within the void mexFunction.
Any and all suggestions will be greatly appreciated. Thank you in advance!

Respuestas (3)

James Tursa
James Tursa el 6 de Jul. de 2011

0 votos

Please post the interface to the C++ function you wish to include in the mex function. i.e., specify what the inputs and outputs are.
Ilya
Ilya el 7 de Jul. de 2011
[EDIT: 20110722 22:08 CDT - reformat - WDR]
Sorry for the delayed response. Here is the code in C++
/*
INPUT: mass_W, mass_S, density, activity, time_step, temp
OUTPUT: mass_W, mass_S, density, activity
*/
#include "mex.h"
#define printf mexPrintf
#include "RC_helper.h"
#include <iostream>
#include <string>
#include "RC_helper.cpp"
using namespace std;
void pipe(double &mass_W, double &mass_S, double &density, double &activity, string time_step, string temp)
{
/**************************************************************/
/************************ Code for GWB ************************/
/**************************************************************/
char* script1[] =
{
"reset",
"temperature = 7",
"swap e- for O2(aq)",
"swap CO2(g) for HCO3-",
"1 free kg H2O",
"13.05 pe",
".006 fugacity CO2(g)",
"balance on H+",
".1 mg/kg H+",
"230 mg/kg Cl-",
"200 mg/kg Ca++",
"243 mg/kg Mg++",
"27 mg/kg K+",
"447 mg/kg Fe++",
"2000 mg/kg SO4--",
"184 mg/kg Na+",
"react -999.999 gram of H2O",
"fix Eh",
"fix fugacity of CO2(g)",
"delxi = .0001 linear",
"step_increase = 1",
"go",
""
};
/**************************************************************/
/************************ Main Program ************************/
/**************************************************************/
char line1[200];
char discard[20];
char** command1;
Pipe pipe1("pipe1");
/* Interation Variable(s) */
int i;
/* Two copies of react are opened */
cout << "Open two copies of React." << endl;
OpenGwbApplic(pipe1, "\\Program Files (x86)\\Gwb\\react.exe");
/* This for loops sets one dimension of the two dimensional array that is formed from the
char pointers equal to the two pre-defined structures */
for (command1 = script1, i = 0; **command1; **command1 ? command1++ : 0, i++)
{
if (**command1){
SendCommand(pipe1, *command1);
}
}
/**************************************************************/
/********************** Remaining Masses **********************/
/**************************************************************/
/* The remaining mass of water is reported */
SendCommand(pipe1, "report Wmass H2O", line1, sizeof(line1));
ReceiveLine(pipe1, line1, sizeof(line1));
sscanf(line1, "%lg", &mass_W);
/* The density of the solution is reported */
SendCommand(pipe1, "report soln_density", line1, sizeof(line1));
ReceiveLine(pipe1, line1, sizeof(line1));
sscanf(line1, "%lg", &density);
/* The activity of the water is reported */
SendCommand(pipe1, "report Watact", line1, sizeof(line1));
ReceiveLine(pipe1, line1, sizeof(line1));
sscanf(line1, "%lg", &activity);
/* The mass of the solution is reported */
SendCommand(pipe1, "report soln_mass", line1, sizeof(line1));
ReceiveLine(pipe1, line1, sizeof(line1));
sscanf(line1, "%lg", mass_S);
/* The react programs are closed */
SendCommand(pipe1, "quit");
printf("press return to exit> ");
gets(discard);
/* The pertinent variables are returned */
return;
}
/* This is useless, but I don't know what to replace with it so and still account for the check done by the following function */
int main()
{
cout << "hello world" << endl;
return 0;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
{
if( main() )
mexErrMsgTxt("main returned non-zero value");
}

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Jul. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by