MATLAB mex例程未返回正确的值
MATLAB mex routine does not return correct value
我正在尝试构建一个供MATLAB调用的C/C++函数。这是我的C/C++代码:
#include "mex.h"
double* MyarrayProduct(double* a, double* b, int N)
{
double* c = (double*)malloc(N*sizeof(double));
double* pc = c;
for (int n = 0; n < N; n++)
{
*pc = *a**b;
pc++;
a++;
b++;
}
return c;
}
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
//------------------------------------------ Verify MEX-File Input and Output Parameters -----------------------------------------------------//
//To check for two input arguments, multiplier and inMatrix, use this code.
if(nrhs!=2)
{
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs",
"Two inputs required.");
}
//Use this code to check for one output argument, the product outMatrix.
if(nlhs!=1)
{
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs",
"One output required.");
}
if( !mxIsDouble(prhs[1]) ||
mxIsComplex(prhs[1]))
{
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble",
"Input matrix must be type double.");
}
/* check that number of rows in second input argument is 1 */
if(mxGetM(prhs[1])!=1)
{
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector",
"Input must be a row vector.");
}
//------------------------------------------ variable declarations here ----------------------------------------//
double *inMatrixA; /* input scalar */
double *inMatrixB; /* 1xN input matrix */
int ncols; /* size of matrix */
double *outMatrix; /* output matrix */
//------------------------------------------- read in data -----------------------------------------//
/* get the value of the scalar input */
inMatrixA = mxGetPr(prhs[0]);
/* create a pointer to the real data in the input matrix */
inMatrixB = mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
ncols = mxGetN(prhs[1]);
//------------------------------------------- Prepare Output Data -----------------------------------------//
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(1,ncols,mxREAL);
/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(plhs[0]);
/* call the computational routine */
outMatrix = MyarrayProduct(inMatrixA,inMatrixB,ncols);
/* code here */
}
这是我的MATLAB教程。
>> a =[1 2 3]
a =
1 2 3
>> b = [4 5 6]
b =
4 5 6
>> mex MyarrayProduct.cpp
>> c = MyarrayProduct(a,b)
c =
0 0 0
我输入了我的C/C++代码,在找到了
outMatrix = MyarrayProduct(inMatrixA,inMatrixB,ncols);
outMatrix
实际上是4、10、18,这是正确的。但它似乎很难将结果发回。我想知道这里出了什么问题?我不能在mex中返回指针?
plhs[0] = mxCreateDoubleMatrix(1,ncols,mxREAL);
在上面的行上,mxCreateDoubleMatrix
调用已经为您分配了一个大小为1 x ncols
的矩阵。
outMatrix = MyarrayProduct(inMatrixA,inMatrixB,ncols);
这里所做的只是重写outMatrix
的值,即它所指向的内存位置。实际上,您并没有覆盖mxCreateDoubleMatrix
分配的内存。
要解决此问题,请将MyarrayProduct()
函数更改为:
void MyarrayProduct(double* a, double* b, int N, double* pc)
{
for (int n = 0; n < N; n++)
{
*pc = *a**b;
pc++;
a++;
b++;
}
}
然后称之为
/* call the computational routine */
MyarrayProduct(inMatrixA,inMatrixB,ncols,outMatrix);
在保持MyarrayProduct()
不变(大部分(的情况下,另一个应该有效的解决方案是:
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(1,0,mxREAL); // don't allocate any memory yet
/* call the computational routine */
outMatrix = MyarrayProduct(inMatrixA,inMatrixB,ncols);
/* free the existing real array (in case MATLAB allocates a size 0 array) */
mxFree(mxGetPr(plhs[0]));
/* reassign the array in the matrix */
mxSetPr(plhs[0], outMatrix);
/* set the correct dimensions */
mxSetN(plhs[0], ncols);
然而,即使在这种情况下,我也会修改MyarrayProduct()
,使其使用mxMalloc
而不是malloc
。链接的MathWorks文档警告您不要使用后者。
在MATLAB应用程序中使用
mxMalloc
而不是ANSI Cmalloc
函数来分配内存。
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- MEX:如何从C++/C向MATLAB返回矩阵
- 在mex函数中返回双指针2D矩阵
- 如何从mex函数返回矩阵结构
- MATLAB mex例程未返回正确的值
- 从c++ MEX文件返回值到MATLAB