Search code examples
cmatlabimage-processingmatrixmex

preparing output data in the main MEX gateway function


I am writing a wrapper function using mex which is used to call the C function. Generally, I used to create the output matrix in the main gateway function using

plhs[0] = mxCreateDoubleMatrix(r,c,mxREAL);

or

plhs[0] = mxCreateNumericArray(3, dim_array, mxDOUBLE_CLASS, mxREAL);

to store the output image. Now I have a function which returns only long integer value instead of an image. How do I handle this output? Do I need to create a matrix for a single value output or are there any other function?


Solution

  • It depends on what you mean by "long integer". Follow the table below to select the type of your newly created array (mxClassID) to match the desired MATLAB and C type. To cheat a little, you can use mxClassIDFromClassName to get the class id from the MATLAB type (e.g. mxClassIDFromClassName('single') to select mxSINGLE_CLASS)

    enter image description here

    If you mean you want to create a scalar, just create an array of size 1-by-1 with mxCreateNumericMatrix:

    mxCreateNumericMatrix(1, 1, mxClassIDFromClassName('int32'), mxREAL)
    

    Also note that there is a convenience function for creating a double scalar, mxCreateDoubleScalar:

    mxArray *ps = mxCreateDoubleScalar(initialValue);
    

    All of the mxCreate* functions are listed on the MathWorks reference page Create or Delete Array.