## Key Features

With MATLAB Coder™, you can integrate the generated code into your projects as source code, static libraries, or dynamic libraries. An example main function is provided as a template to help you incorporate generated code into your application. You can also integrate external code with MATLAB code intended for code generation. The external code can be external libraries, object files, or C/C++ source code.

Integrate code generated by MATLAB ® Coder™ into a parent Microsoft ® Visual Studio ® project.

By using MATLAB Coder with Embedded Coder®, you can further optimize code efficiency and customize the generated code. You can then verify the numerical behavior of the generated code using software-in-the-loop (SIL) and processor-in-the-loop (PIL) execution.

MATLAB Coder works with Simulink Coder™ to generate C code from Simulink® models that contain MATLAB code.

You can also integrate your MATLAB programs (including graphical elements) as custom applications written in C/C++, .NET, Java®, and Python® and deploy them to desktop, web, or enterprise systems using MATLAB Compiler SDK™. MATLAB Central provides a detailed comparison of using these approaches.

MATLAB Coder app (left) and code generation report (right) showing generated C code.

## MATLAB Language and Toolbox Support for Code Generation

MATLAB Coder generates code from a broad range of MATLAB language features that design engineers typically use for developing algorithms as components of larger systems. This includes more than 1200 operators and functions from MATLAB and companion toolboxes, including:

MATLAB Language and Toolbox Support for Code Generation

## Use Cases for MATLAB Coder

MATLAB Coder enables design engineers developing algorithms in MATLAB to generate readable and portable C/C++ code. With this generated code, you can:

• Integrate your MATLAB algorithms as a compiled library component into other software such as a custom simulator
• Accelerate computationally intensive portions of your MATLAB code by generating a MATLAB executable (MEX function) that calls the compiled C/C++ code
• Prototype your MATLAB algorithms as a standalone executable on PCs and communicate the generated code as design specifications to your software engineers
• Implement and verify your MATLAB algorithms on an embedded processor
Integrate code generated by MATLAB ® Coder™ into a parent Microsoft ® Visual Studio ® project.
Generate a MEX-file to accelerate simulation of a DCT-based image compression or decompression algorithm.
Generate code and create an executable to prototype on a desktop PC.
Integrate code generated by MATLAB Coder™ into an iPhone or iPad app using Apple’s Xcode IDE.
MATLAB Coder use cases.

## Generating Code from MATLAB

Translating MATLAB algorithms to C code involves specifying implementation requirements. The MATLAB Coder app and equivalent command-line functions guide you through this iterative process while enabling you to stay within the MATLAB language.

MATLAB Coder helps you prepare your MATLAB algorithm for code generation by analyzing your MATLAB code to propose data type and sizes for your inputs. You can ensure that your algorithm is ready for code generation by generating a MEX function that wraps the compiled code for execution back within MATLAB. MATLAB Coder produces a report that identifies any errors you need to fix to make your MATLAB algorithm ready for code generation. You iterate between fixing errors and regenerating a MEX function until your MATLAB algorithm is suitable for code generation.

You can then generate either C or C++ source code or a MEX function tuned for performance from your MATLAB algorithm.

Explore gallery (4 images)

## MEX Function Generation for Code Verification and Acceleration

The MEX function can be called in place of the original MATLAB code to:

• Test and verify the compiled code back in MATLAB
• Accelerate the execution

As a part of the three-step iterative workflow, you need to generate and test the MEX function to verify that it provides the same functionality as the original MATLAB code.

Testing the MEX function before generating code enables you to detect and fix run-time errors that are much harder to diagnose in the generated code. Running your MEX function in MATLAB executes memory integrity checks that perform array bounds checking, dimension checking, and detect violations of memory integrity in code generated for MATLAB functions. If a violation is detected, MATLAB stops execution and provides a diagnostic message.

The MATLAB Coder app tests by using the same inputs used to run the original MATLAB code and comparing the results from the original MATLAB code with results from the MEX function.

Explore gallery (2 images)

### Accelerating the Execution of MATLAB Algorithms

As a part of an overall strategy to accelerate your MATLAB algorithm, generating a MEX function to replace your original MATLAB code can increase execution speed.

Generate a MEX-file to accelerate simulation of a DCT-based image compression or decompression algorithm.

The amount of acceleration achieved depends on the nature of the algorithm. The best way to determine the acceleration is to follow best practices, generate a MEX-function using MATLAB Coder, and test the speedup first hand. You are likely to see speedups if your algorithm contains the following:

• Single-precision data types
• Fixed-point data types
• Loops with states
• Code that cannot be vectorized

On the other hand, speedups are less likely if your algorithm contains MATLAB implicitly multithreaded computations such as fft and svd, functions that call IPP or BLAS libraries, functions optimized for execution in MATLAB on a PC such as FFTs, or algorithms where you can vectorize the code.

For some applications, you can also combine different techniques such as using vectorization and pre-allocation, System objects™, and Parallel Computing Toolbox™ with MEX function generation to take advantage of multicore processors and accelerate your MATLAB algorithm.

In this webinar you will learn how to use to various techniques to accelerate your communications system simulations in MATLAB and Simulink.

## Multicore-Capable Code Generation Using OpenMP

MATLAB Coder can generate multicore-capable code from parfor-loops. A parfor-loop, like the standard MATLAB for-loop, executes a series of statements over a range of values. Since the iterations of the parfor-loop run in parallel on multiple cores, an iteration of your loop must not depend on other iterations. If MATLAB Coder determines that this requirement is not satisfied, it will treat the parfor-loops as standard for-loops.

MATLAB Coder uses the Open Multiprocessing (OpenMP) application interface to support shared-memory, multicore code generation from MATLAB code written with parfor-loops. The generated code requires a compiler that supports the OpenMP application interface. While MATLAB Coder uses as many cores as are available, you can specify the number of threads to use. If you want distributed parallelism, use Parallel Computing Toolbox.

Example of generated code with calls to OpenMP.

You can use Simulink Coder and Embedded Coder to extend the capabilities of
MATLAB Coder.

### MATLAB Coder with Simulink Coder

The MATLAB Function block for simulation and code generation lets you integrate MATLAB code into Simulink models. Simulink Coder lets you generate code from these Simulink models that contain MATLAB code.

Radar tracking model in Simulink. The model implements a Kalman filtering algorithm written in MATLAB and called using the MATLAB Function block.

### MATLAB Coder with Embedded Coder

By using MATLAB Coder with Embedded Coder, you can further optimize code efficiency and customize the generated code. Embedded Coder generates code for supported embedded processors, on-target rapid prototyping boards, and microprocessors used in mass production. It extends MATLAB Coder and Simulink Coder by providing configuration options and advanced optimizations for fine-grained control of the generated code’s functions, files, and data. Embedded Coder improves code efficiency and facilitates integration with legacy code, data types, and calibration parameters used in production. You can then verify the numerical behavior of the generated code using SIL and PIL execution.

Explore gallery (2 images)