MEX功能评估中的所有零
1view (last 30 days)
显示较旧的评论
克劳迪娅·奇亚罗(Claudia Chiarro)
on 31 Aug 2021
评论:
克劳迪娅·奇亚罗(Claudia Chiarro)在2021年9月2日
Hello everyone,
I would like to create one mex function that implements a function defined in a C file.
The C code of the desired function is the following:
/* myFunc */
整数myFunc(type_md模式,float* u1,float* u2)
{...
return地位;
}
This function is defined in a C file scr.c with some #include statements which I didn't write.
type_Md is an enumeration type.
要创建MEX函数,我运行了一个MATLAB脚本,该脚本包括所需的C文件并评估MEXFUNCTICT:
%编译myFunc_mex
mexfile ='myfunc_mex.c';
mexname ='myfunc_mex';
%包括所有文件
...。。
libfile ='Library.lib';
%汇编单个精度
eval(['MEX -LSRC -L'libfile(1:end-4)''mexfile' -输出 'mexname'_单身的');
基于myFunc()的定义,我创建了C文件myfunc_mex.c,如下:
#include“ mex.h”
#include“ Math.h”
#include“ scr.c”
#include...。
#define MXCLASSmxsingle_class
voidmexfunction(int nlhs,mxarray *plhs [],
intnrhs, constmxarray *prhs [])
{
type_md模式;
整数状态;
FLOAT *u1, *u2;
Mode = (type_Md)mxGetScalar(prhs[0]);
PLHS [0] = mxCreatenumericMatrix(((mwsize)1,1,mxint16_class,0);/* 地位 */
PLHS [1] = mxCreatenumericMatrix(((mwsize)1,1,mxClass,mxreal);/ * u1 */
plhs[2] = mxCreateNumericMatrix((mwSize)1,1,MXCLASS,mxREAL); /* u2 */
status = (INTEGER)mxGetScalar(plhs[0]);
u1 =(float *)mxgetPr(plhs [1]);
u2 =(float *)mxgetPr(plhs [2]);
status = myFunc(mode,u1,u2);
}
Running the Matlab script I don't get any error, the creation of the mex function is successful. But, evaluating the mex function,
[状态,U1,U2] = feval('myFunc_mex_single',模式1)
I don't get the right result, I get that status, u1 and u2 are ALL ZEROS.
此外,如果在myfunc_mex.c中,我用
status = 5;
again I get all zeros. Then I would say that the problem is not (only) in the evaluation of myFunc(), but I'm wrong in defining myFunc_mex.c.
这是我第一次看到MEX功能,我不知道错误在哪里。
0评论
接受的答案
沃尔特·罗伯森(Walter Roberson)
on 31 Aug 2021
In order to return values, you need to store the values inside the mx arrays that you are creating.
您的
U1
and
U2
看起来很适合作为要写入的区域的指针,但是您没有显示任何写入其中的代码。
您的mxGetScalar is reading a
value
从您创建的MXINT16_CLASS数组中。您没有在其中存储任何东西,因此MXGETSCALAR之后的状态将为0。然后,您覆盖本地变量
status
无论返回什么功能,您都永远不会将该状态写入第一个数组的内存中 - 您将需要MXGETPR来获取指针,然后您必须在该指针上间接存储状态。