Mx matrix assignment in mixed programming of VC and matleb (C language)
Convert the x and y data of the vector container to the mxMatrix of the matlab matrix:
1:
memcpy(mxGetPr(mxa_x),&x[0], y.size()*sizeof(double));memcpy(mxGetPr(mxa_y),&y[0], y.size()*sizeof(double));//OK
& X [0]: 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 (): indicates the first address of the element of the vector container.
4. Use the copy Function
copy(x.begin(),x.end(),mxGetPr(mxa_x));copy(y.begin(),y.end(),mxGetPr(mxa_y));//OK
5.
Operation functions of libraries using matlab Functions
MxSetPr (mxa_x, & x [0]); mxSetPr (mxa_y, & y [0]); // OK, ** but cannot release memory space **
6.
The mxSetData function is not implemented yet.
The Code is as follows:
Bool ellipsefit_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 release 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 the matrix pointer in matlab to the pointer to the double in C Language Xc = 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 ;}