关于C#:将变量(float *)保存到plh

Save a variable (float *) to plhs

我有一个代码。但是我不知道如何保存输出(点*)。那就是我尝试将float * out保存到plhs。打印的结果是正确的。有没有可用的示例,因为我找不到合适的示例。

感谢您的回答。我没有任何错误。但是打印出来的结果是正确的。但是在Matlab中,全为零。

我初始化out都为零。但是在我调用pointwise_search之后,out不变。

如果我使用out = pointwise_search(q,p,num_thres,x,len),问题就解决了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 #include"mex.h"
    #include"matrix.h"
    #include <iostream>
    #include
    #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\
"
,len);
        mexPrintf("num_thres=%d\
"
,num_thres);


        double * Numbers= (double *)mxGetData(prhs[0]);
        double * Q= (double *)mxGetData(prhs[1]);
        mexPrintf("Q[4]=%f\
"
,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\
"
,out[0]);
        mexPrintf("out[4]=%f\
"
,out[1]);
        mexPrintf("out[4]=%f\
"
,out[2]);



    }


您不应直接分配out值。

检查一下(C代码就足够了):

首先,使用不同的变量和类型映射MEX功能接口存储器。

然后,将它们全部传递给实际的C函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
}

最后,C函数应通过指针设置out值。

1
2
3
4
5
6
// 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;
    }

哦,这是double写的。对于float和其他类型,请使用mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL)而不是mxCreateDoubleMatrix(1, 1, mxREAL)

这里也是要检查的链接。