All zeros from mex function evaluation
显示 更早的评论
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 */
INTEGER myFunc(type_Md Mode, FLOAT* u1, FLOAT* u2)
{ ...
return status;
}
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.
To create the mex function I run a Matlab script that includes the needed C files and evaluates the mexFunction:
% Compile myFunc_mex
mexfile = 'myFunc_mex.c';
mexname = 'myFunc_mex';
% Including all files
.....
libfile = 'library.lib';
% Compile in single precision
eval(['mex -Lsrc -l' libfile(1:end-4) ' ' mexfile ' -output ' mexname '_single']);
Based on the definition of myFunc(), I create the C file myFunc_mex.c like this:
#include "mex.h"
#include "math.h"
#include "scr.c"
#include ....
#define MXCLASS mxSINGLE_CLASS
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
type_Md Mode;
INTEGER status;
FLOAT *u1, *u2;
Mode = (type_Md)mxGetScalar(prhs[0]);
plhs[0] = mxCreateNumericMatrix((mwSize)1,1,mxINT16_CLASS,0); /* status */
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,
[status,u1,u2] = feval('myFunc_mex_single',Mode1)
I don't get the right result, I get that status, u1 and u2 are ALL ZEROS.
Moreover, if in myFunc_mex.c I substitute the last row with
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.
This is the first time I see a mex function, I don't know where the error would be.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!