| MATLAB Compiler | ![]() |
This section illustrates an advanced example of how to write C code that calls a compiled M-file. Consider a stand-alone application whose source code consists of two files:
multarg.m, which contains a function named multarg.multargp.c, which contains a C function named main.multarg.m specifies two input parameters and returns two output parameters.
function [a,b] = multarg(x,y) a = (x + y) * pi; b = svd(svd(a));
The code in multargp.c calls mlfMultarg and then displays the two values that mlfMultarg returns.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "matlab.h"
#include "multpkg.h"/* Include Compiler-generated header file */
static void PrintHandler( const char *text )
{
printf(text);
}
int main( ) /* Programmer written coded to call mlfMultarg */
{
#define ROWS 3
#define COLS 3
mxArray *a, *b, *x, *y;
double x_pr[ROWS * COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
double x_pi[ROWS * COLS] = {9, 2, 3, 4, 5, 6, 7, 8, 1};
double y_pr[ROWS * COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
double y_pi[ROWS * COLS] = {2, 9, 3, 4, 5, 6, 7, 1, 8};
double *a_pr, *a_pi, value_of_scalar_b;
multpkgInitialize();/* Call multpkg initialization */
/* Install a print handler to tell mlfPrintMatrix how to
* display its output.
*/
mlfSetPrintHandler(PrintHandler);
/* Create input matrix "x" */
x = mxCreateDoubleMatrix(ROWS, COLS, mxCOMPLEX);
memcpy(mxGetPr(x), x_pr, ROWS * COLS * sizeof(double));
memcpy(mxGetPi(x), x_pi, ROWS * COLS * sizeof(double));
/* Create input matrix "y" */
y = mxCreateDoubleMatrix(ROWS, COLS, mxCOMPLEX);
memcpy(mxGetPr(y), y_pr, ROWS * COLS * sizeof(double));
memcpy(mxGetPi(y), y_pi, ROWS * COLS * sizeof(double));
/* Call the mlfMultarg function. */
a = (mxArray *)mlfMultarg(&b, x, y);
/* Display the entire contents of output matrix "a". */
mlfPrintMatrix(a);
/* Display the entire contents of output scalar "b" */
mlfPrintMatrix(b);
/* Deallocate temporary matrices. */
mxDestroyArray(a);
mxDestroyArray(b);
multpkgTerminate();/* Call multpkg termination */
return(0);
}
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);
The mlfMultarg function returns matrices a and b. a has both real and imaginary components; b is a scalar having only a real component. The program uses mlfPrintMatrix to output the matrices, for example,
mlfPrintMatrix(a);
| Simple Example | Controlling Code Generation | ![]() |