Mana V
Mana V

Reputation: 77

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

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
//#define  _tprintf mexPrintf
#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[])
{
//HMODULE hModule(::GetModuleHandle(NULL));
double  valueRead;
//short port;
//short rack;
//short offset;

  //if (hModule != NULL)
  //{
  //  // Initialize MFC and print and error on failure
  //  if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
  //  {
  //    _tprintf(_T("Fatal Error: MFC initialization failed\n"));
  //  }
  //  else
  //  {
  //    //while(true)
  //    //{
  //      valueRead = PortRead(1, 780, -1);
        valueRead = PortRead(port[0], rack[0], offset[0]);
        mexPrintf("Value Read = %i\n",valueRead);
  //      //Sleep(1);  // Sleep for 1s so we can see the value on the screen
  //    //}
  //  }
  //}
  //else
  //{
  //  _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
  //}  
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);
}

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 is not working and I have no idea why..

Upvotes: 0

Views: 121

Answers (1)

A Hernandez
A Hernandez

Reputation: 484

Whatever parameters you pass to the mex function become available with the use of int nrhs, const mxArray *prhs[]

Depending on the type of parameter, you need to use different functions. For example, if the first parameter is a double, you can retrieve so:

double p = mxGetScalar(prhs[0]);

http://de.mathworks.com/help/matlab/access-data.html

Upvotes: 2

Related Questions