简体   繁体   English

更改 mex 函数中的常量参数

[英]Changing the constant arguments in mex-function

This question is part of a bigger project I'm working on.这个问题是我正在进行的一个更大项目的一部分。 I have used a simpler mex-function to explain the issue I'm dealing with.我使用了一个更简单的 mex 函数来解释我正在处理的问题。

The requirement is to change the arguments (variables on the RHS) passed to the mex-function.要求是更改传递给 mex 函数的参数(RHS 上的变量)。 This is a necessary requirement.这是必要的要求。 I have been able to change the variable in case of double * as argumjents.我已经能够在双 * 作为参数的情况下更改变量。 Here's the code:这是代码:

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

}

When I try to do the same thing using typecasting to int * it doesn't work.当我尝试使用对 int * 的类型转换来做同样的事情时,它不起作用。 Here's the code that I have tried:这是我尝试过的代码:

include "mex.h"包括“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);
}

I need to convert double to int * in the case of my project and solving it on this simple example will solve the issue.在我的项目中,我需要将 double 转换为 int * 并在这个简单的例子上解决它会解决这个问题。 Any suggestions?有什么建议吗?

Casting a pointer to a different type does not convert the data it points to to that type.将指针转换为不同类型不会将其指向的数据转换为该类型。 Almost all Matlab data is arrays of double .几乎所有的 Matlab 数据都是double数组。 If your function requires an array of int , you'll need to allocate a separate array for the int s and convert the elements one at a time.如果您的功能需要的数组int ,你需要分配的一个单独的数组int S和转换元件一次一个。

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

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