Main Content

Generate C Code from Simple MATLAB Function

This example shows how to generate C code from a simple MATLAB® function at the command line using the codegen command.

Create Simple Function

Create a simple function to compute the matrix product and element-by-element product of a matrix and its transpose. Add the %#codegen directive to prompt the MATLAB Code Analyzer to identify warnings and errors specific to code generation. Specify the input as a 5x5 matrix of doubles using an arguments block.

type matMult.m
function [elementProduct, matrixProduct]  = matMult(A) %#codegen
arguments
    A (5,5) double
end
elementProduct = A .* A';
matrixProduct = A * A';
end

Create a sample matrix and run matMult in MATLAB.

A = magic(5);
[x,y] = matMult(A)
x = 5×5

   289   552     4    80   165
   552    25    42   168   288
     4    42   169   380   550
    80   168   380   441     6
   165   288   550     6    81

y = 5×5

        1155         870         715         690         795
         870        1055         845         765         690
         715         845        1105         845         715
         690         765         845        1055         870
         795         690         715         870        1155

Generate MEX Function for Testing

Generate a MEX function from matMult using the codegen command. The codegen command checks that the MATLAB function is suitable for code generation and generates a MEX function that you can test in MATLAB prior to generating C code.

codegen matMult
Code generation successful.

By default, codegen generates a MEX function named matMult_mex in the current folder. Run the MEX function to check for run-time errors and to compare the MEX function output to the MATLAB function output.

[x_mex,y_mex] = matMult_mex(A)
x_mex = 5×5

   289   552     4    80   165
   552    25    42   168   288
     4    42   169   380   550
    80   168   380   441     6
   165   288   550     6    81

y_mex = 5×5

        1155         870         715         690         795
         870        1055         845         765         690
         715         845        1105         845         715
         690         765         845        1055         870
         795         690         715         870        1155

Generate and Inspect C code

Generate C code packaged as a standalone C library from matMult using the codegen command with the -config:lib option. You can find the generated C code in the codegen/lib/matMult folder. Inspect the generated C code and compare it to the MATLAB code.

codegen -config:lib matMult
Code generation successful.
type codegen/lib/matMult/matMult.c
/*
 * File: matMult.c
 *
 * MATLAB Coder version            : 23.2
 * C/C++ source code generated on  : 19-Aug-2023 11:14:34
 */

/* Include Files */
#include "matMult.h"

/* Function Definitions */
/*
 * Arguments    : const double A[25]
 *                double elementProduct[25]
 *                double matrixProduct[25]
 * Return Type  : void
 */
void matMult(const double A[25], double elementProduct[25],
             double matrixProduct[25])
{
  double elementProduct_tmp[25];
  int i;
  int i1;
  int i2;
  for (i = 0; i < 5; i++) {
    for (i1 = 0; i1 < 5; i1++) {
      elementProduct_tmp[i1 + 5 * i] = A[i + 5 * i1];
    }
  }
  for (i = 0; i < 25; i++) {
    elementProduct[i] = A[i] * elementProduct_tmp[i];
  }
  for (i = 0; i < 5; i++) {
    for (i1 = 0; i1 < 5; i1++) {
      double d;
      d = 0.0;
      for (i2 = 0; i2 < 5; i2++) {
        d += A[i + 5 * i2] * elementProduct_tmp[i2 + 5 * i1];
      }
      matrixProduct[i + 5 * i1] = d;
    }
  }
}

/*
 * File trailer for matMult.c
 *
 * [EOF]
 */

See Also

|

Related Topics