Convert vector container x, y data to several assignment methods of matlab matrix Mxmatrix:
1:
memcpy(mxGetPr(mxa_x),&x[0], y.size()*sizeof(double));memcpy(mxGetPr(mxa_y),&y[0], y.size()*sizeof(double));//OK
&x[0]: That is, the first address of the container element
2.
memcpy(mxGetPr(mxa_x),&x.at(0), y.size()*sizeof(double));memcpy(mxGetPr(mxa_y),&y.at(0), y.size()*sizeof(double));//OK
3.
memcpy(mxGetPr(mxa_x),(void*)&(*x.begin()), y.size()*sizeof(double));memcpy(mxGetPr(mxa_y),(void*)&(*y.begin()), y.size()*sizeof(double));//OK
& (*x.begin ()): The first address of the element that represents the vector container
4 Using the Copy function
copy(x.begin(),x.end(),mxGetPr(mxa_x));copy(y.begin(),y.end(),mxGetPr(mxa_y));//OK
5.
Operation functions for libraries using MATLAB functions
mxSetPr(mxa_x,&x[0]);mxSetPr(mxa_y,&y[0]);//OK,**但是不能释放内存空间**
6.
Mxsetdata function is temporarily not implemented
The code is as follows:
BOOLEllipsefit_engine (Engine *ep,Double&XC,Double&YC,Double&a,Double&b,Double&phi,Double&p, vector<double>X vector<double>Y) {Mxarray *mxa_x, *mxa_y; Mxarray *mxa_xc = NULL,*MXA_YC = null,*mxa_a = Null,*mxa_b = Null,*mxa_phi = Null,*mxa_p = NULL; mxa_x = Mxcreatedoublematrix (X.size (),1, mxreal); mxa_y = Mxcreatedoublematrix (Y.size (),1, mxreal);//memcpy (MXGETPR (mxa_x), &x[0], y.size () *sizeof (double)); //memcpy (MXGETPR (mxa_y), &y[0], y.size () *sizeof (double));//ok //memcpy (MXGETPR (mxa_x), &x.at (0), y.size () *sizeof (double)); //memcpy (MXGETPR (mxa_y), &y.at (0), y.size () *sizeof (double));//ok //memcpy (MXGETPR (mxa_x), (void*) & (*x.begin ()), Y.size () *sizeof (double)); //memcpy (MXGETPR (mxa_y), (void*) & (*y.begin ()), Y.size () *sizeof (double));//ok //copy (X.begin (), X.end (), MXGETPR (mxa_x)); //copy (Y.begin (), Y.end (), MXGETPR (mxa_y));//ok //MXSETPR (mxa_x,&x[0]); //MXSETPR (mxa_y,&y[0])//ok, but cannot free memory space //mlfellipsefit (6, &mxa_xc,&mxa_yc,&mxa_a,&mxa_b,&mxa_phi,&mxa_p, mxa_x, mxa_x);Engputvariable (EP,"x", mxa_x); Engputvariable (EP,"Y", mxa_y); Mxdestroyarray (mxa_x); Mxdestroyarray (mxa_y); Engevalstring (EP,"UserPath (' C:\Users\Administrator\Desktop\eclipse_c\test ');"); Engevalstring (EP,"[Xc,yc,a,b,phi,p]=ellipsefit (x, y);"); Mxa_xc=enggetvariable (EP,"Xc"); Mxa_yc=enggetvariable (EP,"Yc"); Mxa_a=enggetvariable (EP,"A"); Mxa_b=enggetvariable (EP,"B"); Mxa_phi=enggetvariable (EP,"Phi"); Mxa_p=enggetvariable (EP,"P");Double*P_XC = MXGETPR (MXA_XC);//pass pointer to the matrix in MATLAB to a pointer to double in the C languagexc=p_xc[0];Double*P_YC =MXGETPR (MXA_YC); Yc = p_yc[0];Double*p_a =MXGETPR (mxa_a); A = p_a[0];Double*p_b =MXGETPR (Mxa_b); B = p_b[0];Double*p_phi =MXGETPR (Mxa_phi); Phi = p_phi[0];Double*p_p =MXGETPR (mxa_p); P = p_p[0];return true;}
VC and Matleb mixed programming MX matrix Assignment (C language)