[英]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 * 的類型轉換來做同樣的事情時,它不起作用。 這是我嘗試過的代碼:
/* 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.