Main Content

Generating Code

Generate code and build standalone libraries and executables

C/C++ code generation is the core functionality of MATLAB® Coder™. Generate code from the app, which you can open with coder, or from the command line, by using the codegen function. The app provides step-by-step guidance through the code generation process, including guidance around best practices, and clear visual aids for the many code generation settings and options. The command line workflow enables simple, repeatable, and efficient initiation of code generation. The code generation workflow supports many different data types, including variable-size data and global data. The workflow also supports multiple entry-point (top-level) functions, multisignature MEX function generation, and customization of fundamental properties such as the array layout and array indexing of the generated code. In addition, you can generate code that uses key C++ language features such as classes, namespaces, and function overloading.

Apps

expand all

MATLAB CoderGenerate C code or MEX function from MATLAB code

Functions

expand all

coderOpen MATLAB Coder app
codegenGenerate C/C++ code from MATLAB code
coder.targetDetermine if code generation target is specified target
coder.allowpcodeControl code generation from protected MATLAB files
coder.configCreate MATLAB Coder code generation configuration objects
coder.extrinsicDeclare extrinsic functions
coder.loadLoad compile-time constants from MAT-file or ASCII file into caller workspace
coder.nullcopyDeclare uninitialized variables in code generation
coder.newtypeCreate a coder.Type object to represent the type of an entry-point function input
coder.varsizeDeclare variable-size data
coder.typeofCreate coder.Type object to represent the type of an entry-point function input
coder.resizeResize coder.Type object
coder.cevalCall external C/C++ function
coder.cincludeInclude header file in generated code
coder.cstructnameName C structure type in generated code
coder.opaqueDeclare variable in generated code
coder.refIndicate data to pass by reference
coder.rrefIndicate read-only data to pass by reference
coder.wrefIndicate write-only data to pass by reference

Objects

expand all

coder.MexCodeConfigConfiguration parameters for MEX function generation from MATLAB code
coder.CodeConfigConfiguration parameters for C/C++ code generation from MATLAB code
coder.EmbeddedCodeConfigConfiguration parameters for C/C++ code generation from MATLAB code with Embedded Coder
coder.OutputTypeOutput type from an entry-point function to specify as an input type

Topics

Generating Code Basics

Code Generation Workflow

Generate C/C++ code from MATLAB code.

Generate C Code by Using the MATLAB Coder App

Generate C/C++ code from MATLAB code by using the MATLAB Coder app.

Generate C Code at the Command Line

Generate C/C++ code from MATLAB code by using the codegen command.

C++ Code Generation

Understand code generation considerations specific to C++.

Set Up a MATLAB Coder Project

Set up a project in the MATLAB Coder app.

Switch Between Command Line and Project Workflows

Convert codegen Command to Equivalent MATLAB Coder Project

Use the codegen command with the -toproject option.

Convert MATLAB Coder Project to MATLAB Script

Generate code at the command line by using project settings.

Share Build Configuration Settings

Export project settings to a code generation configuration object or import the settings into a project.

Data-specific Workflows

Generate Code for Variable-Size Data

Generate code for data whose size might change at run time.

Generate Code for Global Data

Generate C/C++ code from MATLAB code that uses global data

Generate Code for Enumerations

Generate code from MATLAB code that uses enumerations.

Extended Functionality

Generate Code for Multiple Entry-Point Functions

Generate C/C++ code for multiple entry-point functions.

Generate One MEX Function That Supports Multiple Signatures

Generate a single MEX function for multiple signatures.

Pass an Entry-Point Function Output as an Input

Simplify input type specification for multiple entry-point functions.

Generate Code That Uses Row-Major Array Layout

Generate C/C++ code with row elements stored contiguously in memory.

Generate Code That Uses N-Dimensional Indexing

Preserve array dimensions in generated code.

Extended C++ Functionality

Generate C++ Code with Class Interface

Generate C++ code that is packaged into a class.

Generate C++ Classes for MATLAB Classes

Generate a C++ class for a value class, handle class, or System object™ in your MATLAB code.

Use Dynamically Allocated C++ Arrays in the Generated Function Interfaces

Understand and use dynamically allocated arrays from the generated C++ function interfaces.

Organize Generated C++ Code into Namespaces

Namespaces organize the generated code into logical parts and prevent name collisions.

Code Appearance

Reserved Keywords

Certain words in your code, that are C/C++ keywords, might be renamed in the generated code.

Featured Examples