How to return float value from mex function and how to get it from m file?

I understand that all the return values โ€‹โ€‹of the mex function are stored in a plhs array of type mxArray *. I want to return a value of type float. How can i do this?

Some code examples for returning it from the mex function and extracting it from the m file are very much appreciated.

+6
source share
1 answer

The class name MATLAB for data of type float is "single".

In the MEX file you can write:

void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { // Create a 2-by-3 real float plhs[0] = mxCreateNumericMatrix(2, 3, mxSINGLE_CLASS, mxREAL); // fill in plhs[0] to contain the same as single([1 2 3; 4 5 6]); float * data = (float *) mxGetData(plhs[0]); data[0] = 1; data[1] = 4; data[2] = 2; data[3] = 5; data[4] = 3; data[5] = 6; } 

Extracting it from an M file is pretty much like calling any other function. If you called the MEX function foo , you would call it like this:

 >> x = foo; 

Now x will contain a single precision value equivalent to single([1 2 3; 4 5 6]) , which was stored in plhs[0] .

+8
source

Source: https://habr.com/ru/post/887676/


All Articles