User`s manual
Advanced Topics
2-43
format, which takes twice the space. See the allocation of zout in the
example that follows.
2 Once the variable is returned to MATLAB, convert its storage so that it is
compatible with MATLAB. Use the
fort2mat function for this.
Example – Passing Complex Variables. The example below shows how to call an
LAPACK function from MATLAB, passing complex
prhs[0] as input and
receiving complex
plhs[0] as output. Temporary variables zin and zout are
used to hold
prhs[0] and plhs[0] in Fortran format.
#include "mex.h"
#include "fort.h" /* defines mat2fort and fort2mat */
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, mxArray
*prhs[] )
{
int lda, n;
double *zin, *zout;
lda = mxGetM( prhs[0] );
n = mxGetN( prhs[0] );
/* Convert input to Fortran format */
zin = mat2fort( prhs[0], lda, n );
/* Allocate a real, complex, lda-by-n variable to store output */
zout = mxCalloc( 2*lda*n, sizeof(double) );
/* Call complex LAPACK function */
zlapack_function( zin, &lda, &n, zout );
/* Convert output to MATLAB format */
plhs[0] = fort2mat( zout, lda, lda, n );
/* Free intermediate Fortran format arrays */
mxFree( zin );
mxFree( zout );
}