2014-07-23 6 views
-2

C++ 함수와 관련 mex를 작성합니다. 그러나 C++ 함수의 한 종류의 입력은 double *입니다.오류가 'void *'에서 'float *'로 변환 될 수 없습니다.

  1. 기능 pointwise_search의 출력은 포인터입니다. 나는 그것을 삭제해야한다고 들었다. 그러나 출력으로 필요하기 때문에 어디에서 삭제해야하는지 모르겠습니다.

  2. 대답에서 나는 입력 유형을 mxIsSingle으로 확인해야한다는 것을 알고 있습니다. 그래서 함수를 수정합니다 mexFunction. 그러나 오류 error C2440: '=' : cannot convert from 'void *' to 'float *'이 있습니다.

  3. 매트랩, I는 pointwise_search( 플로트 * 피 , 플로트 Q , num_thres, 플로트, len) N 형 호한다. 만약 내가 벡터 v_in_matlab = rand (5,1) matlab에있다. 나는 그것이 p=single(v_in_matlab);에 의해 포인터와 사전에 다음 pointwise_search(p...

감사 받아야합니다.

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

답변

1

Matlab에서는 mexFunction을 호출하기 전에 데이터를 변환 할 때 single()을 사용합니다. C++ 측에서는 유형이 실제로는 mxIsSingle()에 의해 단일인지 확인하십시오. 이 후 행복하게 float*으로 전송할 수 있습니다.

1

MEX 코드에 대해 걱정하기 전에 먼저 C++ 기능을 다시 살펴보십시오. 정말 확실한 몇 가지가 있습니다. memory leaks (new, 아니요, delete[]).

MEX과 관련이 볼해서는 안 :

(float *)mxGetPr(prhs[0]) 

당신은 float*double* 캐스트와 숫자가 어떤 의미를 기대할 수 없다. MATLAB에서 입력 single 사용 :

(float *)mxGetData(prhs[0]) 

그리고 Trilarion이 예상 데이터 유형에 대한 mxArray의를 제안하고 모든 입력을 테스트로 않습니다.

+0

귀하의 의견에 감사드립니다. 'pointwise_search' 함수의 출력 'Y'를 어디에서 삭제해야하는지 말해 주시겠습니까? – Vivian

관련 문제