User`s guide
Mixing M-Files and C or C++
4-49
You can build this program into a stand-alone application by using the
command
mcc -t -W lib:multpkg -T link:exe multarg multargp.c
libmmfile.mlib
The program first displays the contents of a 3-by-3 matrix a and then displays
the contents of scalar
b:
6.2832 +34.5575i 25.1327 +25.1327i 43.9823 +43.9823i
12.5664 +34.5575i 31.4159 +31.4159i 50.2655 +28.2743i
18.8496 +18.8496i 37.6991 +37.6991i 56.5487 +28.2743i
143.4164
An Explanation of This C Code
Invoking the MATLAB Compiler on multarg.m generates the C function
prototype:
extern mxArray * mlfMultarg(mxArray * * b, mxArray * x,
mxArray * y);
extern void mlxMultarg(int nlhs, mxArray * plhs[], int nrhs,
mxArray * prhs[]);
This C function header shows two input arguments (mxArray *x and
mxArray *y) and two output arguments (the return value and mxArray **b).
Use
mxCreateDoubleMatrix to create the two input matrices (x and y). Both x
and
y contain real and imaginary components. The memcpy function initializes
the components, for example:
x = mxCreateDoubleMatrix(ROWS, COLS, COMPLEX);
memcpy(mxGetPr(x), x_pr, ROWS * COLS * sizeof(double));
memcpy(mxGetPi(y), y_pi, ROWS * COLS * sizeof(double));
The code in this example initializes variable x from two arrays (x_pr and x_pi)
of predefined constants. A more realistic example would read the array values
from a data file or a database.
After creating the input matrices,
main calls mlfMultarg:
a = (mxArray *)mlfMultarg(&b, x, y);