All zeros from mex function evaluation
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!