User`s manual

Examples of C MEX-Files
2-11
/* Call the timestwo_alt subroutine. */
timestwo_alt(y,x);
}
This example passes the input scalar x by value into the timestwo_alt
subroutine, but passes the output scalar
y by reference.
Passing Strings
Any MATLAB data type can be passed to and from MEX-files. For example,
this C code accepts a string and returns the characters in reverse order.
/* $Revision: 1.10 $ */
/*=============================================================
* revord.c
* Example for illustrating how to copy the string data from
* MATLAB to a C-style string and back again.
*
* Takes a string and returns a string in reverse order.
*
* This is a MEX-file for MATLAB.
* Copyright (c) 1984-2000 The MathWorks, Inc.
*============================================================*/
#include "mex.h"
void revord(char *input_buf, int buflen, char *output_buf)
{
int i;
/* Reverse the order of the input string. */
for(i = 0; i < buflen-1; i++)
*(output_buf+i) = *(input_buf+buflen-i-2);
}
In this example, the API function mxCalloc replaces calloc, the standard C
function for dynamic memory allocation.
mxCalloc allocates dynamic memory
using MATLAB’s memory manager and initializes it to zero. You must use
mxCalloc in any situation where C would require the use of calloc. The same
is true for
mxMalloc and mxRealloc; use mxMalloc in any situation where C
would require the use of
malloc and use mxRealloc where C would require
realloc.