User`s manual
2 Creating C Language MEX-Files
2-44
Preserving Input Values from Modification
Many LAPACK and BLAS functions modify the values of arguments passed in
to them. It is advisable to make a copy of arguments that can be modified prior
to passing them to the function. For complex inputs, this point is moot since the
mat2fort version of the input is a new piece of memory, but for real data this
is not the case.
The following example calls an LAPACK function that modifies the first input
argument. The code in this example makes a copy of
prhs[0], and then passes
the copy to the LAPACK function to preserve the contents of
prhs[0].
/* lapack_function modifies A so make a copy of the input */
m = mxGetM( prhs[0] );
n = mxGetN( prhs[0] );
A = mxCalloc( m*n, sizeof(double) );
/* Copy mxGetPr( prhs[0] ) into A */
temp = mxGetPr( prhs[0] );
for ( k = 0; k < m*n; k++ ) {
A[k] = temp[k];
}
/* lapack_function does not modify B so it is
OK to use the input
directly */
B = mxGetPr( prhs[1] );
lapack_function( A,B ); /* modifies A but not B */
/* Free A when you are done with it */
mxFree(A);
Building the MEX-File
The examples in this section show how to compile and link a C MEX file,
myCmexFile.c, on the platforms supported by MATLAB. In each example, the
term
<matlab> stands for the MATLAB root directory.
If you build your C MEX-file on a PC or IBM_RS platform, you need to
explicitly specify a library file to link with.
On the PC, use either one of the following.
mex myCmexFile.c <matlab>/extern/lib/win32/digital/df60/
libmwlapack.lib