Then, whenever mxMalloc is called, mxMalloc calls your memory allocation routine instead of malloc . By default, in a MEX-file, mxMalloc generates nonpersistent mxMalloc data. In other words, the memory management facility automatically deallocates the memory as soon as the MEX-file ends. If you want the memory to persist after the MEX-file completes, call mexMakeMemoryPersistent after calling mxMalloc . When you finish using the memory allocated by mxMalloc , call mxFree .
Examples See mxmalloc.c in the mx subdirectory of the examples directory.
For an additional example, see mxsetdimensions.c in the mx subdirectory of the examples directory.