Save a variable (float *) to plhs

人走茶凉 提交于 2019-12-13 03:01:53

问题


I have a code.But I do not know how to save the output(point *). That is I try to save float * out to plhs. The result printed is right. Is there any available example since I did not find a suitable one.

Thanks for the answer. I do not have any error. But the result printed is right. But in Matlab, out is all zeros.

I initialize out are all zeros. But After I call pointwise_search, out is no change.

The problem is solved if I use out = pointwise_search(q,p,num_thres,x,len).

 #include "mex.h"
    #include "matrix.h"
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <vector>

    using namespace std;


    void pointwise_search(double *p,double *q,int num_thres, double* n, int len, double * out  )
    {
        vector<double> P(p, p + num_thres);
        vector<double> Q(q, q + num_thres);
        int size_of_threshold = P.size();
        double * Y;
        double *z=new double[len];
        typedef vector<double > ::iterator IntVectorIt ;
        IntVectorIt start, end, it, location ;
        start = P.begin() ;   // location of first
        // element of Numbers

        end = P.end() ;       // one past the location
        // last element of Numbers

        for (int i=0;i<len;i++)
        {
            location=lower_bound(start, end, n[i]) ;
            z[i]=location - start;
            if(z[i]>0&&z[i]<=size_of_threshold-1)
            {

                out[i]=(n[i]-P[z[i]])/(P[z[i]-1]-P[z[i]])*(Q[z[i]-1]-Q[z[i]])+Q[z[i]];
            }
            else if (z[i]>size_of_threshold-1)
            {
                out[i]=Q[z[i]-1];
            }
            else
            {
                out[i]=Q[z[i]];
            }
        }

        delete []z;

    }


    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {
        double * out;

        double *n = (double*) mxGetData(prhs[3]);
        int len = (int) mxGetScalar(prhs[4]);
        int num_thres = (int) mxGetScalar(prhs[2]);
        mexPrintf("len=%d\n  ",len);
        mexPrintf("num_thres=%d\n  ",num_thres);


        double * Numbers= (double *)mxGetData(prhs[0]);
        double * Q= (double *)mxGetData(prhs[1]);
        mexPrintf("Q[4]=%f\n  ",Q[4]);

        plhs[0] = mxCreateNumericMatrix(len, 1,mxSINGLE_CLASS, mxREAL);  /* Create the output matrix */
       out = (double *)mxGetPr(plhs[0]);
        pointwise_search(Numbers,Q,num_thres,n,len,out );
        mexPrintf("out[4]=%f\n  ",out[0]);
        mexPrintf("out[4]=%f\n  ",out[1]);
        mexPrintf("out[4]=%f\n  ",out[2]);



    }

回答1:


You shouldn't assign out value directly.

Check it out (C-code should be enough):

First, you map MEX-function interface memory with different variables and types.

Then, you pass all of them to the actual C-function.

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{
  int num;
  float *A;
  double *out;
  //mxLogical, etc

  /* Extract the inputs */
  num = (int)mxGetScalar(prhs[0]);
  A = (float *)mxGetData(prhs[1]);
  // You can get sizes of A with mxGetM/mxGetN functions

  /* Setup the output */
  // It's 1x1 matrix of doubles here.
  plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
  out = mxGetPr(plhs[0]);

  /* Do the actual work */
  // If you need to iterate over A, pass M,N values here also
  your_function(num, A, out);
}

At the end, C-function should set out value by pointer.

// And declare M,N here as inputs
void your_function(const int num, const float* A, double *out)
    {
      //Some code. Operate with `num`, `A`, etc
      *out = DBL_MAX;
    }

Oh, wrote this one for double. For float and other types use mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL) instead of mxCreateDoubleMatrix(1, 1, mxREAL).

Here is also a link to check.



来源:https://stackoverflow.com/questions/24931805/save-a-variable-float-to-plhs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!