-2

I write a c++ function and its associated mex. But the one kind of input of c++ function is double *.

  1. The output of function pointwise_search is a pointer. I was told that I should delete it. But I do not know where I should delete it since I need it as an output.

  2. From the answer, I know I should check the type of input by mxIsSingle. So I correct the function mexFunction. But there is an error error C2440: '=' : cannot convert from 'void *' to 'float *'.

  3. In matlab, I should call like pointwise_search(float *p,float q, num_thres,float n, len ). If I have a vector v_in_matlab=rand(5,1) in matlab. I should I get it pointer by p=single(v_in_matlab); and then pointwise_search(p...

Thanks in advance.

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

using namespace std;


float * pointwise_search(float *p,float *q,int num_thres, float* n, int len )
{
    vector<float> P(p, p + num_thres);
    vector<float> Q(q, q + num_thres);
    int size_of_threshold = P.size();
    float  *Y=new float[len];
    float *z=new float[len];
    typedef vector<float > ::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)
        {

            Y[i]=(n[i]-P[z[i]])/(P[z[i]-1]-P[z[i]])*(Q[z[i]-1]-Q[z[i]])+Q[z[i]];
        }
        else
        {
            Y[i]=Q[z[i]];
        }
    }

    return (&Y[0]);
}




 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
   {
    float * Numbers, *Q;
    if (nrhs != 5)
    {
        mexErrMsgTxt("Input is wrong!");
    }
    float *n = (float*) mxGetData(prhs[3]);
    int len = (int) mxGetScalar(prhs[4]);
    int num_thres = (int) mxGetScalar(prhs[2]);

    /* Input gs */

    if(mxIsComplex(prhs[0])
    ||!mxIsSingle(prhs[0]))
        mexErrMsgTxt("Input 0 should be a class Single");
    /* get the pointer to gs */
    Numbers=mxGetData(prhs[0]);


    if(mxIsComplex(prhs[0])
    ||!mxIsSingle(prhs[0]))
        mexErrMsgTxt("Input 0 should be a class Single");
    /* get the pointer to gs */
    Q=mxGetData(prhs[1]);

//     float * Numbers= (float *)mxGetData(prhs[0]);
//     float * Q= (float *)mxGetData(prhs[1]);

    float * out= pointwise_search(Numbers,Q,num_thres,n,len );
    //float* resizedDims = (float*)mxGetPr(out);
}
Vivian
  • 207
  • 1
  • 3
  • 11

2 Answers2

1

In Matlab use single() to convert the data before calling the mexFunction. On the C++ side verify that the type is indeed single by mxIsSingle(). After this you can happily cast to float*.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
1

Before you worry about your MEX code, have another look at your C++ function first. You have some really obvious memory leaks (new but no delete[]).

Regarding MEX you should never see this:

(float *)mxGetPr(prhs[0])

You can't cast a double* to a float* and expect the numbers to make any sense. Input single from MATLAB and use:

(float *)mxGetData(prhs[0])

And do as Trilarion suggests and test all of your input mxArrays for the expected data type.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • thank you for your comment. Could you tell me where I should delete the output `Y` of the function `pointwise_search` – Vivian Jul 24 '14 at 08:16