Main Content

Differences in Appearance of Generated Code and MATLAB Code

MATLAB® Coder™ translates and optimizes dynamically typed MATLAB code into statically typed C/C++. Statically typed languages require variable types to be explicitly declared and these types are determined at compile time. Certain changes and optimizations performed by the code generator enable the use of MATLAB data types and features in the generated code. For more information on data types and features supported for code generation, see Data Definition and MATLAB Language Features Supported for C/C++ Code Generation.

These changes and optimizations cause the generated code to appear differently than the MATLAB code. The generated code might not map in a one-to-one manner with your MATLAB code due to any of the following:

Note

Depending on your source code, these cases might occur slightly differently than how they are shown here.

Mapping MATLAB Functions to C/C++ Functions

MATLAB Coder generates standalone C/C++ code and MEX code from MATLAB code. A single function in your MATLAB code might be translated into multiple functions in the generated code. Two or more functions in your MATLAB code might also become one function body in the generated code. This process is called function inlining. By default, the code generator uses internal heuristics to determine whether to inline your functions or not. For more information, see Control Inlining to Fine-Tune Performance and Readability of Generated Code.

Representation of Function Outputs

Outputs of a MATLAB function might become return values in C or might become pass-by-reference inputs. One scalar output in your MATLAB code is treated as a return value in the generated code.

  • The function addOne has an input variable x and output variable y. For this example, x is of type double.

    function y = addOne(x)
    y = x + 1;
    end

    The code generated for the snippet is shown here:

    double addOne(double x)
    {
      return x + 1.0;
    }

    The input to the function addOne, x, is treated as a pass-by-value variable in the generated code. The output of the MATLAB function is returned by value in the generated code.

  • For arrays, outputs might be passed-by-reference. The code snippet shown here uses a double input x and an array output y.

    function y = addMat(x)
    z = [1:100];
    y = z + x;
    end
    

    The output variable y is translated into a pass-by-reference array variable in the generated code shown here:

    void addMat(double x, double y[100])
    {
      int i;
      for (i = 0; i < 100; i++) {
        y[i] = ((double)i + 1.0) + x;
      }
    }
  • For entry-point functions that have multiple output variables, outputs might be passed-by-reference in the generated code. This code snippet has two double scalar outputs, y and z, with a double scalar input x.

    function [z,y] = splitOne(x)
    y = x + 1;
    z = x + 2;
    end
    

    The output variables y and z are translated into a pass-by-reference variables in the generated code:

    void splitOne(double x, double *z, double *y)
    {
      *y = x + 1.0;
      *z = x + 2.0;
    }

For more information on argument passing behavior of entry-point functions in the generated code, see Deploy Generated Code.

Constant Values Removed in Generated Code

Constant values in your code might not be preserved in generated code. These values might get removed to optimize the generated code. Constant folding removes the computations that might have been present in your MATLAB code and replaces the computations with the result instead. For more information, see Constant Folding.

Consider this code snippet:

function y = removeConst
x = ones(10);
y = x + 1;
end

The code generator removes the constant matrix x to save memory and assigns the constant value as the result. The generated code looks like this code:

void removeConst(double y[100])
{
  int i;
  for (i = 0; i < 100; i++) {
    y[i] = 2.0;
  }
}

Unused inputs or constant inputs to nonentry-point functions in your MATLAB code are removed from the function bodies in the generated code.

Function specializations by the code generator can change the function to a version where the input type, size, complexity, or value might be customized for a particular invocation of the function. This is done to produce efficient C code at the expense of code duplication. For more information, see Specialized Functions or Classes.

Accessing Matrix Elements

In the preceding instance, accessing a matrix requires extra lines of code in C/C++. A 10-by-10 matrix is represented as an array of 100 double elements in the generated code. A for loop is used to access the array elements. C/C++ do not support many matrix operations, so the code generator converts the matrices and the operations on matrices to arrays and methods like for loops to access those arrays.

Math Operations and Other Function Calls

The generated code might use standard C libraries to carry out the math operations or other functions in your MATLAB code. For a list of supported language functions, see MATLAB Language Features Supported for C/C++ Code Generation.

Variable-Size Arrays

For code generation, an array can be fixed-size or variable-size. Variable-size arrays might appear in different formats in the generated code. Code can be generated for a fixed-size array if the code generator can determine the size of the array. Code generation is also valid for a fixed-size array with an upper bound. Dynamically allocated arrays are also generated in certain cases. See Code Generation for Variable-Size Arrays.

Code generation for fixed-size and variable-size arrays might yield the following variable declarations in the generated code:

double x[10];        // Fixed-size array
double y_data[20];
int y_size[2];       // y_data and y_size denote an upper-bounded array
emxArrayReal_T *z;   // Dynamically allocated array

Local Variables in Generated Code

If your MATLAB code contains local variables that occupy a lot of memory, then in the generated code they might be declared as local variables, static local variables, or as variables in a struct that is passed into your entry-point function in your generated code. You can control this transformation by controlling the memory that is allocated for the generated code. See Control Stack Space Usage.

Cell Arrays in Generated Code

To implement cell arrays in generated code, the code generator might translate them as a struct, static array, or dynamic array. For more information, see Code Generation for Cell Arrays.

Initialize and Terminate Functions

The code generator might produce two housekeeping functions, initialize and terminate. You can find these functions in the Generated Code tab in the code generation report. The initialize function initializes the state on which the generated C/C++ entry-point functions operate. The terminate function frees allocated memory and performs other cleanup operations. For more information, see Use Generated Initialize and Terminate Functions

Related Topics