User`s guide
Mixing M-Files and C or C++
4-41
/* Print the results. */
mlfPrintMatrix(R);
/* Free the matrices allocated during this computation. */
mxDestroyArray(N);
mxDestroyArray(R);
PkgTerminate(); /* Terminate the library of M-functions */
}
An Explanation of mrankp.c
The heart of mrankp.c is a call to the mlfMrank function. Most of what comes
before this call is code that creates an input argument to
mlfMrank.Mostof
what comes after this call is code that displays the vector that
mlfMrank
returns. First, the code must call the Compiler-generated library initialization
function.
PkgInitialize();/* Initialize the library of M-Functions */
To understand how to call mlfMrank, examine its C function header, which is
mxArray *mlfMrank(mxArray *n_rhs_)
According to the function header, mlfMrank expects one input parameter and
returns onevalue. All input and output parameters are pointe rs to the
mxArray
datatype. (See“ExternalInterfaces/API”fordetails on the mxArray datatype.)
To create and manipulate
mxArray * variables in your C code, you can call the
mx routines described in the “External Interfaces/API” or any routine in the
MATLAB C/C++ Math Library. For example, to create a 1-by-1
mxArray *
variable named N with real data, mrankp calls mlfScalar.
N = mlfScalar(n);
mrankp
can now call mlfMrank, passing the initialized N asthesoleinput
argument.
R = mlfMrank(N);
mlfMrank
returns a pointer to an mxArray * variable named R. The easiest way
to display the contents of
R is to call the mlfPrintMatrix convenience function.
mlfPrintMatrix(R);