简体   繁体   中英

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.

The requirement is to change the arguments (variables on the RHS) passed to the mex-function. 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. Here's the code that I have tried:

include "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. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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