MATLAB: How to Create Basic MEX-functions in CMEX Files

1603MATLABtechnote

I want a basic example on how to create a C Mex File in MATLAB.

Best Answer

Many people have requested a simple example on how to create a C MEX-file in MATLAB 5.0 and later. In response to this request, the attached C MEX-file, named 'mexample', is provided as an introduction to cmex programming. 'mexample' is a commented program which describes how to use the following MEX-functions:
mexErrMsgTxt
mxCreateDoubleMatrix
mxGetM
mxGetN
mxGetPr
mxIsComplex
mxIsSparse
mxIsChar
In MATLAB, mexample accepts two inputs and returns one output. The inputs are a 2-by-2 array denoted as ARRAY_IN and a 2-by-1 vector denoted as VECTOR_IN. The function calculates the determinant of ARRAY_IN, multiplies each element of VECTOR_IN by the determinant, and returns this as the output, denoted by zVECTOR_OUT. All inputs and outputs to this function are assumed to be real (not complex).