将变量(float *)保存到plhs [英] Save a variable (float *) to plhs

查看:138
本文介绍了将变量(float *)保存到plhs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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]);



    }

推荐答案

您不应直接分配out值.

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

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

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

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

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

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

最后,C函数应通过指针设置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;
    }

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

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.

这篇关于将变量(float *)保存到plhs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆