繁体   English   中英

更改 mex 函数中的常量参数

[英]Changing the constant arguments in mex-function

这个问题是我正在进行的一个更大项目的一部分。 我使用了一个更简单的 mex 函数来解释我正在处理的问题。

要求是更改传递给 mex 函数的参数(RHS 上的变量)。 这是必要的要求。 我已经能够在双 * 作为参数的情况下更改变量。 这是代码:

#include "mex.h"
/* The computational routine */
void arrayProduct(double x, double *y, double *z, mwSize n)
    {
    mwSize i;
    /* multiply each element y by x */
    for (i=0; i<n; i++) {
        z[i] = (x * y[i]);
    }
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
              int nrhs, const mxArray *prhs[])
{
double multiplier;              /* input scalar */
double *inMatrix;               /* 1xN input matrix */
size_t ncols;                   /* size of matrix */
double *outMatrix;              /* output matrix */

/* check for proper number of arguments */
if(nrhs!=3) {
    mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Three inputs required.");
}
if(nlhs!=0) {
    mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","Zero output required.");
}

/* get the value of the scalar input  */
multiplier = mxGetScalar(prhs[0]);
/* create a pointer to the real data in the input matrix  */
inMatrix = mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[1]);

/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(prhs[2]);
/* call the computational routine */
arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);

}

当我尝试使用对 int * 的类型转换来做同样的事情时,它不起作用。 这是我尝试过的代码:

包括“mex.h”

/* The computational routine */
void arrayProduct(double x, double *y, int *z, mwSize n)
{
    mwSize i;
    /* multiply each element y by x */
    for (i=0; i<n; i++) {
        z[i] = (x * y[i]);
    }
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
    double multiplier;              /* input scalar */
    double *inMatrix;               /* 1xN input matrix */
    size_t ncols;                   /* size of matrix */
    int *outMatrix;              /* output matrix */
    /* check for proper number of arguments */
    if(nrhs!=3) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Two inputs required.");
    }
    if(nlhs!=0) {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required.");
    }

    /* get the value of the scalar input  */
    multiplier = mxGetScalar(prhs[0]);
    int mult = (int)multiplier;
    /* create a pointer to the real data in the input matrix  */
    inMatrix = mxGetPr(prhs[1]);
   /* int *inMat;
    inMat = *inMatrix;*/
    /* get dimensions of the input matrix */
    ncols = mxGetN(prhs[1]);
    /* create the output matrix */
    /* get a pointer to the real data in the output matrix */
    outMatrix = (int *)mxGetData(prhs[2]);
    /* call the computational routine */
    arrayProduct(multiplier,inMatrix,outMatrix,(mwSize)ncols);
}

在我的项目中,我需要将 double 转换为 int * 并在这个简单的例子上解决它会解决这个问题。 有什么建议吗?

将指针转换为不同类型不会将其指向的数据转换为该类型。 几乎所有的 Matlab 数据都是double数组。 如果您的功能需要的数组int ,你需要分配的一个单独的数组int S和转换元件一次一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM