MATLAB Compiler    

Advanced C Example

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 specifies two input parameters and returns two output parameters:

The code in multargp.c calls mlfMultarg and then displays the two values that mlfMultarg returns.

You can build this program into a stand-alone application by using the command

The program first displays the contents of a 3-by-3 matrix a and then displays the contents of scalar b:

An Explanation of This C Code

Invoking the MATLAB Compiler on multarg.m generates the C function prototype:

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:

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:

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:


  Simple Example Controlling Code Generation