All zeros from mex function evaluation

12 Ansichten (letzte 30 Tage)
Claudia Chiaro
Claudia Chiaro am 31 Aug. 2021
Kommentiert: Claudia Chiaro am 2 Sep. 2021
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 31 Aug. 2021
In order to return values, you need to store the values inside the mx arrays that you are creating.
Your u1 and u2 look appropriate to be pointers to the area to write into, but you do not show any code that writes into them.
Your mxGetScalar is reading a value out of the mxINT16_CLASS array that you created. You did not store anything in there, so status is going to be 0 after the mxGetScalar. You then overwrite the local variable status with whatever the function returned, but you never write that status into the memory of that first array -- you would have needed an mxGetPr to get the pointer to it, and then you would have had to store status indirect on that pointer.
  3 Kommentare
Walter Roberson
Walter Roberson am 2 Sep. 2021
#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_ptr;
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_ptr = (INTEGER*)mxGetPtr(plhs[0]);
u1 = (FLOAT *)mxGetPr(plhs[1]);
u2 = (FLOAT *)mxGetPr(plhs[2]);
status_ptr[0] = myFunc(Mode, u1, u2);
}
Claudia Chiaro
Claudia Chiaro am 2 Sep. 2021
Thank you very much. I forgot to update the post, it works.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by