User`s manual
Examples of C MEX-Files
2-21
Handling Complex Data
Complex data from MATLAB is separated into real and imaginary parts.
MATLAB’s API provides two functions,
mxGetPr and mxGetPi, that return
pointers (of type
double *) to the real and imaginary parts of your data.
This example takes two complex row vectors and convolves them.
/* $Revision: 1.8 $ */
/*=========================================================
* convec.c
* Example for illustrating how to pass complex data
* from MATLAB to C and back again
*
* Convolves two complex input vectors.
*
* This is a MEX-file for MATLAB.
* Copyright (c) 1984-2000 The MathWorks, Inc.
*=======================================================*/
#include "mex.h"
/* Computational subroutine */
void convec( double *xr, double *xi, int nx,
double *yr, double *yi, int ny,
double *zr, double *zi)
{
int i,j;
zr[0] = 0.0;
zi[0] = 0.0;
/* Perform the convolution of the complex vectors. */
for(i = 0; i < nx; i++) {
for(j = 0; j < ny; j++) {
*(zr+i+j) = *(zr+i+j) + *(xr+i) * *(yr+j) - *(xi+i)
* *(yi+j);
*(zi+i+j) = *(zi+i+j) + *(xr+i) * *(yi+j) + *(xi+i)
* *(yr+j);
}
}
}