The MATLAB class name for float type data is "single".
In the MEX-file you could 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;
}
Retrieving it from the M-file is pretty much like calling any other function. If you named the MEX-function foo
, you'd call it like this:
>> x = foo;
Now x
would contain the single-precision value equivalent to single([1 2 3; 4 5 6])
that was stored in plhs[0]
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…