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.
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.
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 enables design engineers developing algorithms in MATLAB to generate readable and portable C/C++ code. With this generated code, you can:
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)
The MEX function can be called in place of the original MATLAB code to:
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)
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.
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:
On the other hand, speedups are less likely if your algorithm contains MATLAB implicitly multithreaded computations such as
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.
MATLAB Coder can generate multicore-capable code from
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
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.
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.
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)