How can a MEX file give parameters to a C++ program?

1 Ansicht (letzte 30 Tage)
Mana V
Mana V am 5 Aug. 2015
Bearbeitet: James Tursa am 5 Aug. 2015
Hello!
I am coding a program in MATLAB, in which I have to call a C++ library. To do so, I created a C++ file that calls the library and then a MEX-file that help me to call the desired function in MATLAB. The function I want to access in the library returns me a value, but I have to give it parameters. I am currently able to retrieve a value, because I write my parameteres directly in the C++ code as you can see here (my file's name is Test704()):
// Test704.cpp : Defines the entry point for the console application.
#define _AFXDLL
#include "StdAfx.h"
#include "704IO.h"
#include "Test704.h"
#include "mex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
CWinApp theApp; // The one and only application object
/////////////////////////////////////////////////////////////////////////////
using namespace std;
/////////////////////////////////////////////////////////////////////////////
//int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
int _tmain(double port[], double rack[], double offset[])
{
double valueRead;
//short port;
//short rack;
//short offset;
// valueRead = PortRead(1, 780, -1);
valueRead = PortRead(port[0], rack[0], offset[0]);
mexPrintf("Value Read = %i\n",valueRead);
return valueRead;
}
/////////////////////////////////////////////////////////////////////////////
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *port, *rack, *offset;
// Creates a 1-by-1 real integer.
//plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
int* data = (int*) mxGetData(plhs[0]);
port = mxGetPr(prhs[0]);
rack = mxGetPr(prhs[0]);
offset = mxGetPr(prhs[0]);
//valueRead = mxGetPr(plhs[0]);
//data[0]=_tmain(0,0,0);
//return ;
data[0] = _tmain(port,rack,offset);
return ;
}
You will notice that I have commented parts of the code in order to make researches. Indeed, I want now to be able to give parameters by this way (MATLAB code):
x = 1;
y = 780;
z = 1;
myVal = double(Test704(x,y,z));
and to still be able to retrieve a value in myVal. I helped myself with the example timestwo.c provided by MathWorks, but unfortunately, it just returns me a value of 0 instead of 2 or 4, and I cannot figure out why.
Thanks for help,
Mana

Akzeptierte Antwort

James Tursa
James Tursa am 5 Aug. 2015
Bearbeitet: James Tursa am 5 Aug. 2015
In this line you are using an integer output format %i to print a floating point double value:
mexPrintf("Value Read = %i\n",valueRead);
That will not work. You need to use one of the floating point formats for this. E.g.,
mexPrintf("Value Read = %g\n",valueRead); // or %f or %e
Once that is fixed you will get the proper value printed to the screen and see if it is correct at that point in the code. Also, I find it a bit strange that you are using a double for valueRead even though the function _tmain returns an int.
Then there are typos in your inputs. The following lines of code get the pointers from the 1st argument, not the 1st three arguments:
port = mxGetPr(prhs[0]);
rack = mxGetPr(prhs[0]);
offset = mxGetPr(prhs[0]);
So change that to:
port = mxGetPr(prhs[0]);
rack = mxGetPr(prhs[1]);
offset = mxGetPr(prhs[2]);
And if you want the output to be a double at your m-file level, why not just return a double variable from the mex routine? E.g.,
plhs[0] = mxCreateDoubleScalar( _tmain(port,rack,offset) );

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by