How to correctly pass inputs and get outputs from Mex function?

Viewed 189

This is the first time I try to generate a MEX function. I saw some examples of it but I fail when I try to emulate them. I need to create a MEX function passing through the C code that calls a subroutine where a certain function is evaluated. The C code from which I start is this:

#include "mex.h"
#include "math.h"
#include "source.c"

#define MXCLASS mxSINGLE_CLASS 

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{   
    /* input */
    new_type Index;
    
    /* output */
    INTEGER Flag;
    FLOAT *Result;
    
    Index = (new_type)mxGetScalar(prhs[0]);
    
    plhs[0] = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL); 
    plhs[1] = mxCreateNumericMatrix(1,1,MXCLASS,mxREAL); 
    
    Flag = (INTEGER)mxGetScalar(plhs[0]);
    Result = (FLOAT *)mxGetPr(plhs[1]);
    
    
    Flag = src_Function(Index, Result);

}

where source.c is the C file where the subroutine is defined, src_Function() is the subroutine. The inclusion of src_Function() is correct, because the creation of the MEX function is successful. new_type is an enumerated type defined in source.c. The subroutine is defined in source.c as

INTEGER src_Function(new_type Index, FLOAT* Result)
{
...
return Flag;
}

and Result is computed inside src_Function. The call to the MEX function is

[flag,result] = feval('src_Function_mex',index);

where src_Function_mex.mexw64 is the MEX file that has been created by Matlab. First of all, calling the MEX function, I got that both flag and result were zero. So I did some debug and noticed that I fail in passing the input variable index to the MEX function. In fact, Index inside the C code is zero and has no sense. This leads to an incorrect evaluation of src_Function and so Flag, which says if the computation failed or not, has a valid value (Flag = -1) which reports some failures in the computation. Another error occurs when I retrieve flag as output of the MEX function. Because Flag in the C code is -1, but flag in Matlab is zero. So I believe to be wrong (maybe in the C code) when I collect the input variables and when I try to return the outputs. But I don't know how to correct them. Any help please?

[Edit] Thank you very much for your suggestion, Ander! The elements of enum new_type are like:

typedef enum new_type
{
    Red;
    Blue;
    Yellow;
} new_type;

I tried to edit the MEX function call by passing as input variable the corresponding double

[flag,result] = feval('src_Function_mex',double(index));

where index is a new_type. Now Index in the C code is right and Flag = 1, which means that the evaluation of src_Function is successful.

[Edit] To return Flag I used pointers. It works. Thanks

1 Answers

Input was passed through a cast and output is returned using pointer

#include "mex.h"
#include <math.h>
#include "source.c"

#define MXCLASS mxSINGLE_CLASS 

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{   
    /* input */
    new_type Index;
    
    /* output */
    INTEGER *Flag;
    FLOAT *Result;
    
    Index = (new_type)mxGetScalar(prhs[0]);
    
    plhs[0] = mxCreateNumericMatrix(1,1,mxINT16_CLASS,mxREAL); 
    plhs[1] = mxCreateNumericMatrix(1,1,MXCLASS,mxREAL); 
    
    Flag = (INTEGER *)mxGetPr(plhs[0]);
    Result = (FLOAT *)mxGetPr(plhs[1]);
    
    
    *Flag = src_Function(Index, Result);

}

And the function call is

[flag,result] = feval('src_Function_mex',double(index));
Related