Main Content

Code Generation from MATLAB Code

You can use Navigation Toolbox™ together with MATLAB® Coder™ to:

  • Create a MEX file to speed up your MATLAB application.

  • Generate ANSI®/ISO® compliant C/C++ source code that implements your MATLAB functions and models.

  • Generate a standalone executable that runs independently of MATLAB on your computer or another platform.

In general, the code you generate using the toolbox is portable ANSI C code. In order to use code generation, you need a MATLAB Coder license. For more information, see Get Started with MATLAB Coder (MATLAB Coder).

Using MATLAB Coder

Several Navigation Toolbox functions are enabled to generate C/C++ code. To generate code from robotics functions, follow these steps:

  • Write your function or application that uses Navigation Toolbox functions that are enabled for code generation. For code generation, some of these functions have requirements that you must follow.

  • Add the %#codegen directive to your MATLAB code.

  • Follow the workflow for code generation from MATLAB code using either the MATLAB Coder app or the command-line interface.

Using the MATLAB Coder App

The basic workflow is:

  1. In the MATLAB Coder (MATLAB Coder) App, set up a project. Specify your top-level functions and define input types.

    The app screens your code for code generation readiness. It reports issues such as a function that is not supported for code generation.

  2. Check for run-time issues.

    The app generates and runs a MEX version of your function. This step detects issues that can be hard to detect in the generated C/C++ code.

  3. Configure the code generation settings for your application.

  4. Generate C/C++ code.

  5. Verify the generated C/C++ code. If you have an Embedded Coder® license, you can use software-in-the-loop execution (SIL) or processor-in-the-loop (PIL) execution.

For a tutorial, see Generate Code for Path Planning Using RRT Star Planner.

Using the MATLAB Coder in Command-Line

The basic workflow is:

  • To detect issues and verify the behavior of the generated code, generate a MEX version of your function.

  • Use coder.config to create a code configuration object for a library or executable.

  • Modify the code configuration object properties as required for your application.

  • Generate code using the codegen (MATLAB Coder) command.

  • Verify the generated C/C++ code. If you have an Embedded Coder license, you can use software-in-the-loop execution (SIL) or processor-in-the-loop (PIL) execution.

For a tutorial, see Generate Code for Path Planning Using Hybrid A Star.

C/C++ Compiler Setup

Before using codegen (MATLAB Coder) to compile your code, you must set up your C/C++ compiler. For 32-bit Windows platforms, MathWorks® supplies a default compiler with MATLAB. If your installation does not include a default compiler, you can supply your own compiler. For the current list of supported compilers, see Supported and Compatible Compilers on the MathWorks website. Install a compiler that is suitable for your platform, then read Setting Up the C or C++ Compiler (MATLAB Coder). After installation, at the MATLAB command prompt, run mex -setup. You can then use the codegen (MATLAB Coder) function to compile your code.

Functions and System Objects That Support Code Generation

For an alphabetized list of features supporting C/C++ code generation, see Functions Supporting C++ Code Generation. You can also view the Extended Capabilities section on any reference page.

See Also

Apps

Functions

Related Examples

More About