Mx matrix assignment (C) and matlebmx in mixed programming of VC and matleb

Source: Internet
Author: User

Mx matrix assignment (C) and matlebmx in mixed programming of VC and matleb

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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.