User`s manual

2 Creating C Language MEX-Files
2-12
Note MATLAB automatically frees up memory allocated with the mx
allocation routines (
mxCalloc, mxMalloc, mxRealloc) upon exiting your
MEX-file. If you don’t want this to happen, use the API function
mexMakeMemoryPersistent.
Below is the gateway routine that calls the C computational routine
revord.
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char *input_buf, *output_buf;
int buflen,status;
/* Check for proper number of arguments. */
if(nrhs != 1)
mexErrMsgTxt("One input required.");
else if(nlhs > 1)
mexErrMsgTxt("Too many output arguments.");
/* Input must be a string. */
if(mxIsChar(prhs[0]) != 1)
mexErrMsgTxt("Input must be a string.");
/* Input must be a row vector. */
if(mxGetM(prhs[0]) != 1)
mexErrMsgTxt("Input must be a row vector.");
/* Get the length of the input string. */
buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
/* Allocate memory for input and output strings. */
input_buf = mxCalloc(buflen, sizeof(char));
output_buf = mxCalloc(buflen, sizeof(char));
/* Copy the string data from prhs[0] into a C string
* input_buf. If the string array contains several rows,
* they are copied, one column at a time, into one long
* string array. */