Sending C++ object to MATLAB workspace,and using it in mex on Button Callback

1 Ansicht (letzte 30 Tage)
I want to create c++ Class object and send it to MALTAB workspace and using it through mex file on button callback.
Here is the code
//Header MATLABeng.h
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#ifndef MATLABeng_H
#define MATLABeng_H
#ifndef MATLABeng_DLL
#define MATLABeng_API __declspec( dllimport )
#else
#define MATLABeng_API __declspec( dllexport )
#endif
class CRectangle{
public :
int x,y;
MATLABeng_API CRectangle(int,int);
MATLABeng_API ~CRectangle();
MATLABeng_API int area();
};
#endif
//Source MATLABeng.cxx
#include "MATLABeng.h"
CRectangle::CRectangle(int a,int b)
{
x = a;
y = b;
}
int
CRectangle::area()
{
printf("I am inside area");
printf("%d",x);
printf("%d",y);
int z = x * y;
return (x*y);
}
int main()
{
Engine *ep;
const CRectangle *rect = new CRectangle(10,20);
mxArray *T = NULL, *result = NULL;
if (!(ep = engOpen("\0"))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
int o=(int)rect;
double arr1[1]={o};
mxArray *TempArr = mxCreateDoubleMatrix(1,1,mxREAL);
memcpy((void *)mxGetPr(TempArr), (void *)arr1, sizeof(arr1));
engPutVariable(ep,"rectptr",TempArr);
engEvalString(ep,"f = figure();");
engEvalString(ep,"button=uicontrol(f,'Style','pushbutton');");
engEvalString(ep,"set(button,'Callback',{@buttonpress,rectptr});");
while(1)
{
if(ep == NULL)
exit;
}
}
//mexfunction
#include <stdio.h>
#include "mex.h"
#include "matrix.h"
#include <string.h>
#include<string>
#include "MATLABeng.h"
void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const
mxArray *prhs[ ])
{
mexPrintf("I am inside button press callback");
mexPrintf("\nnrhs : %i\n",nrhs);
if (nrhs != 3) {
mexErrMsgTxt("Three input arguments required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
char buffer[50];
double * getaddptrdata_= mxGetPr(prhs[2]);
double getadddata_=*getaddptrdata_;
int p =getadddata_;
itoa(p,buffer,16);
std::string buf = buffer;
int t = buf.length();
std::string buf1;
if (t == 5)
buf1 = "0x000";
else if(t == 6)
buf1 = "0x00";
else if(t==7)
buf1 = "0x0";
else
buf1 = "0x";
std::string buff = buf1 + buf;
const char * buffer6 = buff.c_str();
CRectangle * rect = (CRectangle *)strtoul(buffer6, 0, 0);
return;
}
I am able to obtain the address but the object members carry
garbage value. And also want use the class function

Antworten (1)

Ken Atwell
Ken Atwell am 30 Mär. 2012
I believe MATLAB Engine works by instantiating a process for MATLAB and communicating via COM. So, the CRectangle you are creating and "passing" via a type-casted pointer to MATLAB would not be available inside the MATLAB process.
You may have better luck if you create the CRectangle in its own "creator" MEX file, and then access it a second MEX file as you are already doing (you would also avoid the need to use a string representation of the CRectangle pointer). Just be careful to manage your heap memory, as MATLAB will have no clue as to what you are up to!

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) 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