请考虑这段代码的摘录 mexFunction
\\cppfunc.cpp
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){
\\...Parts where I check the number of lhs and rhs
double* upper = mxGetPr(prhs[0]);
double* lower = mxGetPr(prhs[1]);
double* grids = mxGetPr(prhs[2]);
mexPrintf("upper 1=%d \n\n", upper[0] );
}
我计划像这样调用我的 mex 函数cppfunc([1 2 3], [1 2 3], [1 2 3])
。基本上,它接受三个MATLAB
向量并通过 打印出第一个输入向量的第一个元素mexPrintf
。输出应该是1
. 但是,它返回:
upper 1=3
.
在我看来,该函数正在返回第一个输入向量的长度。例如,如果我这样做cppfunc([1 2 3 4], [1 2 3], [1 2 3])
,它将返回upper 1=4
.
我认为我%d
在调用中使用是正确的,mexPrintf
因为mxGetPr
将输入转换为双精度数组。我错过了什么?
格式%d
适用于类型为 的整数值int
。你必须使用%f
for double
s。
请参阅printf 的文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句