Main Content

MATLAB Code Design Considerations for Code Generation

When writing MATLAB® code that you want to convert into efficient, standalone C/C++ code, you must consider the following:

  • Data types

    C and C++ use static typing. To determine the types of your variables before use, MATLAB Coder™ requires a complete assignment to each variable.

  • Array sizing

    Variable-size arrays and matrices are supported for code generation. You can define inputs, outputs, and local variables in MATLAB functions to represent data that varies in size at run time.

  • Memory

    You can choose whether the generated code uses static or dynamic memory allocation.

    With dynamic memory allocation, you potentially use less memory at the expense of time to manage the memory. With static memory, you get better speed, but with higher memory usage. Most MATLAB code takes advantage of the dynamic sizing features in MATLAB, therefore dynamic memory allocation typically enables you to generate code from existing MATLAB code without modifying it much. Dynamic memory allocation also allows some programs to compile even when upper bounds cannot be found.

    Static allocation reduces the memory footprint of the generated code, and therefore is suitable for applications where there is a limited amount of available memory, such as embedded applications.

  • Speed

    Because embedded applications must run in real time, the code must be fast enough to meet the required clock rate.

    To improve the speed of the generated code:

    • Choose a suitable C/C++ compiler. Do not use the default compiler that MathWorks® supplies with MATLAB for Windows® 64-bit platforms.

    • Consider disabling run-time checks.

      By default, for safety, the code generated for your MATLAB code contains memory integrity checks and responsiveness checks. Generally, these checks result in more generated code and slower simulation. Disabling run-time checks usually results in streamlined generated code and faster simulation. Disable these checks only if you have verified that array bounds and dimension checking is unnecessary.

See Also