User`s guide

6 Standalone Applications
To create and manipulate mxArray * variables in your C code, you can call the
mx routines described in the External Interfaces documentation. For example,
to create a 1-by-1
mxArray * varia ble n a med N with real data, mrankp calls
mxCreateDoubleScalar.
N = mxCreateDoubleScalar(n);
mrankp
can now call mlfMrank, passing the initialized N asthesoleinput
argument.
R = mlfMrank(1,&R,N);
mlfMrank
returns its output in a n ew ly allocated mxArray * variable named
R.ThevariableR is initialized to NULL. Output variables that have not been
assigned to a valid
mxArray should be set to NULL. The easiest way to display
the contents of
R is to call the mlfPrint matrix function.
mlfPrintmatrix(R);
This function is defined in Printmatrix.m.
Finally,
mrankp must free the heap memory a llocated to hold matrices and
call the termination functions.
mxDestroyArray(N);
mxDestroyArray(R);
libPkgTerminate(); /* Terminate the library of M-functions */
mclTerminateApplication(); /* Ter minate the MCR */
Advanced C Example
This section provides an advanced example that illustrates how to w rite C
code that calls a com piled M-file. Consider a standalone application whose
source code consists of the files:
multarg.m, which contains a function named multarg
multargp.c, which contains C wrapper code that calls the C interface
function for the M-code
printmatrix.m, which contains the helper function to print a matrix to
the screen
6-18