我正在研究这本书Accelerating MATLAB Performance和page 394,这段代码写的是:
#include "mex.h"
void mexFunction (int nlhs,mxArray *plhs[],/*outputs*/
int nrhs, const mxArray *prhs[])/*inputs*/
{
const char *name = mexFunctionName();
printf("s() called with %d inputs,%d outputs\n",name,nrhs,nlhs);
}
基于本书中的内容,在使用命令mex hello.cpp
构建MEX代码之后,应生成以下结果:
>> hello
hello() called with 0 inputs, 0 outputs
>> hello(1,2,3)
hello() called with 3 inputs, 0 outputs
>> [a,b] = hello(1,2,3)
hello() called with 3 inputs, 2 outputs
One or more output arguments not assigned during call to "hello".
但是当我在Win7x64
机器上运行相同的代码时,结果如下:
>> mex hello.cpp
Building with 'Microsoft Visual C++ 2010'.
MEX completed successfully.
>> hello
s() called with 2082650752 inputs,0 outputs
>> hello(1,2,3)
s() called with 2082650752 inputs,3 outputs
>> [a,b] = hello(1,2,3)
s() called with 2082650752 inputs,3 outputs
One or more output arguments not assigned during call to "hello".
造成这些意外结果的原因是什么?
答案 0 :(得分:4)
感谢您提醒我 - 这是在本书编辑阶段输入的拼写错误 - 正确的代码是printf('%s() called...
(即错误地删除了前导%
)。我会相应地更新勘误表。
我希望你发现本书的其他部分很有用。如果您这样做,请post a positive comment on Amazon。
答案 1 :(得分:3)
在
printf("s() called with %d inputs,%d outputs\n",name,nrhs,nlhs);
你有3个参数,但只有两个“%”,所以输出 “s()调用[name]输入,[nrhs]输出” 并且没有使用nlhs。只需删除名称,然后使用
printf("s() called with %d inputs,%d outputs\n",nrhs,nlhs);
或使用%s显示函数名称:
printf("%s called with %d inputs,%d outputs\n",name,nrhs,nlhs);