MATLAB® Coder User's Guide
- Publisher
- MathWorks
- Year
- 2022
- Tongue
- English
- Leaves
- 1476
- Category
- Library
No coin nor oath required. For personal study only.
✦ Table of Contents
About MATLAB Coder
MATLAB Coder Product Description
Product Overview
When to Use MATLAB Coder
Code Generation for Embedded Software Applications
Code Generation for Fixed-Point Algorithms
Design Considerations for C/C++ Code Generation
When to Generate Code from MATLAB Algorithms
When Not to Generate Code from MATLAB Algorithms
Which Code Generation Feature to Use
Prerequisites for C/C++ Code Generation from MATLAB
MATLAB Code Design Considerations for Code Generation
See Also
Differences Between Generated Code and MATLAB Code
Functions that have Multiple Possible Outputs
Writing to ans Variable
Logical Short-Circuiting
Loop Index Overflow
Indexing for Loops by Using Single Precision Operands
Index of an Unentered for Loop
Character Size
Order of Evaluation in Expressions
Name Resolution While Constructing Function Handles
Termination Behavior
Size of Variable-Size N-D Arrays
Size of Empty Arrays
Size of Empty Array That Results from Deleting Elements of an Array
Binary Element-Wise Operations with Single and Double Operands
Floating-Point Numerical Results
NaN and Infinity
Negative Zero
Code Generation Target
MATLAB Class Property Initialization
MATLAB Classes in Nested Property Assignments That Have Set Methods
MATLAB Handle Class Destructors
Variable-Size Data
Complex Numbers
Converting Strings with Consecutive Unary Operators to double
Display Function
Potential Differences Reporting
Addressing Potential Differences Messages
Disabling and Enabling Potential Differences Reporting for MATLAB Coder
Disabling and Enabling Potential Differences Reporting for Fixed-Point Designer
Potential Differences Messages
Automatic Dimension Incompatibility
mtimes No Dynamic Scalar Expansion
Matrix-Matrix Indexing
Vector-Vector Indexing
Loop Index Overflow
MATLAB Language Features Supported for C/C++ Code Generation
MATLAB Features That Code Generation Supports
MATLAB Language Features That Code Generation Does Not Support
Functions, Classes, and System Objects Supported for Code Generation
Functions and Objects Supported for C/C++ Code Generation
Defining MATLAB Variables for C/C++ Code Generation
Variables Definition for Code Generation
Best Practices for Defining Variables for C/C++ Code Generation
Define Variables By Assignment Before Using Them
Use Caution When Reassigning Variables
Use Type Cast Operators in Variable Definitions
Define Matrices Before Assigning Indexed Variables
Index Arrays by Using Constant Value Vectors
Eliminate Redundant Copies of Variables in Generated Code
When Redundant Copies Occur
How to Eliminate Redundant Copies by Defining Uninitialized Variables
Defining Uninitialized Variables
Reassignment of Variable Properties
Reuse the Same Variable with Different Properties
When You Can Reuse the Same Variable with Different Properties
When You Cannot Reuse Variables
Limitations of Variable Reuse
Supported Variable Types
Edit and Represent Coder Type Objects and Properties
Object Properties
Legacy Representation of Coder Type Objects
Defining Data for Code Generation
Data Definition Considerations for Code Generation
Code Generation for Complex Data
Restrictions When Defining Complex Variables
Code Generation for Complex Data with Zero-Valued Imaginary Parts
Results of Expressions That Have Complex Operands
Results of Complex Multiplication with Nonfinite Values
Encoding of Characters in Code Generation
Array Size Restrictions for Code Generation
Code Generation for Constants in Structures and Arrays
Code Generation for Strings
Limitations
Differences Between Generated Code and MATLAB Code
Define String Scalar Inputs
Define String Scalar Types at the Command Line
Define String Scalar Inputs in the MATLAB Coder App
Code Generation for Sparse Matrices
Sparse Data Types in Generated Code
Input Definition
Code Generation Guidelines
Code Generation Limitations
Specify Array Layout in Functions and Classes
Specify Array Layout in a Function
Query Array Layout of a Function
Specify Array Layout in a Class
Code Design for Row-Major Array Layout
Understand Potential Inefficiencies Caused by Array Layout
Linear Indexing Uses Column-Major Array Layout
Code Generation for Variable-Size Data
Code Generation for Variable-Size Arrays
Memory Allocation for Variable-Size Arrays
Enabling and Disabling Support for Variable-Size Arrays
Variable-Size Arrays in a Code Generation Report
Control Memory Allocation for Variable-Size Arrays
Provide Upper Bounds for Variable-Size Arrays
Disable Dynamic Memory Allocation
Configure Code Generator to Use Dynamic Memory Allocation for Arrays Bigger Than a Threshold
Specify Upper Bounds for Variable-Size Arrays
Specify Upper Bounds for Variable-Size Inputs
Specify Upper Bounds for Local Variables
Define Variable-Size Data for Code Generation
Use a Matrix Constructor with Nonconstant Dimensions
Assign Multiple Sizes to the Same Variable
Define Variable-Size Data Explicitly by Using coder.varsize
Diagnose and Fix Variable-Size Data Errors
Diagnosing and Fixing Size Mismatch Errors
Diagnosing and Fixing Errors in Detecting Upper Bounds
Incompatibilities with MATLAB in Variable-Size Support for Code Generation
Incompatibility with MATLAB for Scalar Expansion
Incompatibility with MATLAB in Determining Size of Variable-Size N-D Arrays
Incompatibility with MATLAB in Determining Size of Empty Arrays
Incompatibility with MATLAB in Determining Class of Empty Arrays
Incompatibility with MATLAB in Matrix-Matrix Indexing
Incompatibility with MATLAB in Vector-Vector Indexing
Incompatibility with MATLAB in Matrix Indexing Operations for Code Generation
Incompatibility with MATLAB in Concatenating Variable-Size Matrices
Differences When Curly-Brace Indexing of Variable-Size Cell Array Inside Concatenation Returns No Elements
Variable-Sizing Restrictions for Code Generation of Toolbox Functions
Common Restrictions
Toolbox Functions with Restrictions for Variable-Size Data
Generate Code With Implicit Expansion Enabled
Output Size
Additional Code Generation
Performance Variation
Optimize Implicit Expansion in Generated Code
Disable Implicit Expansion in Specified Function by Using coder.noImplicitExpansionInFunction
Disable Implicit Expansion for Specific Binary Operation by Using coder.sameSizeBinaryOp
Disable Implicit Expansion in your Project
Representation of Arrays in Generated Code
Customize Interface Generation
Control Memory Allocation for Fixed-Size Arrays
Enable Dynamic Memory Allocation for All Fixed-Size Arrays
Enable Dynamic Memory Allocation for Arrays Bigger Than a Threshold
Resolve Error: Size Mismatches
Issue
Possible Solutions
Code Generation for MATLAB Structures
Structure Definition for Code Generation
Structure Operations Allowed for Code Generation
Define Scalar Structures for Code Generation
Restrictions When Defining Scalar Structures by Assignment
Adding Fields in Consistent Order on Each Control Flow Path
Restriction on Adding New Fields After First Use
Define Arrays of Structures for Code Generation
Ensuring Consistency of Fields
Using repmat to Define an Array of Structures with Consistent Field Properties
Defining an Array of Structures by Using struct
Defining an Array of Structures Using Concatenation
Index Substructures and Fields
Assign Values to Structures and Fields
Code Generation for Categorical Arrays
Code Generation for Categorical Arrays
Define Categorical Arrays for Code Generation
Allowed Operations on Categorical Arrays
MATLAB Toolbox Functions That Support Categorical Arrays
Define Categorical Array Inputs
Define Categorical Array Inputs at the Command Line
Define Categorical Array Inputs in the MATLAB Coder App
Representation of Categorical Arrays
Categorical Array Limitations for Code Generation
Code Generation for Cell Arrays
Code Generation for Cell Arrays
Homogeneous vs. Heterogeneous Cell Arrays
Controlling Whether a Cell Array Is Homogeneous or Heterogeneous
Naming the Structure Type That Represents a Heterogeneous Cell Array in the Generated Code
Cell Arrays in Reports
Control Whether a Cell Array Is Variable-Size
Define Cell Array Inputs
Cell Array Limitations for Code Generation
Cell Array Element Assignment
Variable-Size Cell Arrays
Definition of Variable-Size Cell Array by Using cell
Cell Array Indexing
Growing a Cell Array by Using {end + 1}
Cell Array Contents
Passing Cell Arrays to External C/C++ Functions
Code Generation for Datetime Arrays
Code Generation for Datetime Arrays
Define Datetime Arrays for Code Generation
Allowed Operations on Datetime Arrays
MATLAB Toolbox Functions That Support Datetime Arrays
Define Datetime Array Inputs
Define Datetime Array Inputs at the Command Line
Define Datetime Array Inputs in the MATLAB Coder App
Representation of Datetime Arrays
Datetime Array Limitations for Code Generation
Code Generation for Duration Arrays
Code Generation for Duration Arrays
Define Duration Arrays for Code Generation
Allowed Operations on Duration Arrays
MATLAB Toolbox Functions That Support Duration Arrays
Define Duration Array Inputs
Define Duration Array Inputs at the Command Line
Define Duration Array Inputs in the MATLAB Coder App
Representation of Duration Arrays
Duration Array Limitations for Code Generation
Code Generation for Tables
Code Generation for Tables
Define Tables for Code Generation
Allowed Operations on Tables
MATLAB Toolbox Functions That Support Tables
Define Table Inputs
Define Table Inputs at the Command Line
Define Table Inputs in the MATLAB Coder App
Representation of Tables
Table Limitations for Code Generation
Creating Tables Limitations
Modifying Tables Limitations
Using Table Functions Limitations
Code Generation for Timetables
Code Generation for Timetables
Define Timetables for Code Generation
Allowed Operations on Timetables
MATLAB Toolbox Functions That Support Timetables
Define Timetable Inputs
Define Timetable Inputs at the Command Line
Define Timetable Inputs in the MATLAB Coder App
Representation of Timetables
Timetable Limitations for Code Generation
Creating Timetables Limitations
Modifying Timetables Limitations
Using Timetable Functions Limitations
Code Generation for Enumerated Data
Code Generation for Enumerations
Define Enumerations for Code Generation
Allowed Operations on Enumerations
MATLAB Toolbox Functions That Support Enumerations
Customize Enumerated Types in Generated Code
Specify a Default Enumeration Value
Specify a Header File
Include Class Name Prefix in Generated Enumerated Type Value Names
Generate C++11 Code Containing Ordinary C Enumeration
Code Generation for MATLAB Classes
MATLAB Classes Definition for Code Generation
Language Limitations
Code Generation Features Not Compatible with Classes
Defining Class Properties for Code Generation
Inheritance from Built-In MATLAB Classes Not Supported
Classes That Support Code Generation
Generate Code for MATLAB Value Classes
Generate Code for MATLAB Handle Classes and System Objects
Code Generation for Handle Class Destructors
Guidelines and Restrictions
Behavioral Differences of Objects in Generated Code and in MATLAB
Class Does Not Have Property
Solution
Passing By Reference Not Supported for Some Properties
Handle Object Limitations for Code Generation
A Variable Outside a Loop Cannot Refer to a Handle Object Allocated Inside a Loop
A Handle Object That a Persistent Variable Refers To Must Be a Singleton Object
References to Handle Objects Can Appear Undefined
System Objects in MATLAB Code Generation
Usage Rules and Limitations for System Objects for Generating Code
System Objects in codegen
System Objects in the MATLAB Function Block
System Objects in the MATLAB System Block
System Objects and MATLAB Compiler Software
Specify Objects as Inputs at the Command Line
Consistency Between coder.ClassType Object and Class Definition File
Limitations for Using Objects as Entry-Point Function Inputs
Specify Objects as Inputs in the MATLAB Coder App
Automatically Define an Object Input Type
Provide an Example
Consistency Between the Type Definition and Class Definition File
Limitations for Using Objects as Entry-Point Function Inputs
Work Around Language Limitation: Code Generation Does Not Support Object Arrays
Issue
Possible Solutions
Generating C++ Classes
Generate C++ Classes for MATLAB Classes
Example: Generate Code for a Handle Class That Has Private and Public Members
Additional Usage Notes and Limitations
Code Generation for Function Handles
Function Handle Limitations for Code Generation
Code Generation for Deep Learning Arrays
Code Generation for dlarray
Define dlarray for Code Generation
dlarray Object Functions with Code Generation Support
Deep Learning Toolbox Functions with dlarray Code Generation Support
MATLAB Functions with dlarray Code Generation Support
dlarray Limitations for Code Generation
Recommended Usage
Limitations
Defining Functions for Code Generation
Code Generation for Variable Length Argument Lists
Generate Code for arguments Block That Validates Input Arguments
Supported Features
Input Type Specification and arguments blocks
Specify Number of Entry-Point Function Input or Output Arguments to Generate
Control Number of Input Arguments
Control the Number of Output Arguments
Code Generation for Anonymous Functions
Anonymous Function Limitations for Code Generation
Code Generation for Nested Functions
Nested Function Limitations for Code Generation
Calling Functions for Code Generation
Resolution of Function Calls for Code Generation
Key Points About Resolving Function Calls
Compile Path Search Order
When to Use the Code Generation Path
Resolution of File Types on Code Generation Path
Compilation Directive %#codegen
Use MATLAB Engine to Execute a Function Call in Generated Code
When To Declare a Function as Extrinsic
Use the coder.extrinsic Construct
Call MATLAB Functions Using feval
Working with mxArrays
Restrictions on Using Extrinsic Functions
Code Generation for Recursive Functions
Compile-Time Recursion
Run-Time Recursion
Disallow Recursion
Disable Run-Time Recursion
Recursive Function Limitations for Code Generation
Force Code Generator to Use Run-Time Recursion
Treat the Input to the Recursive Function as a Nonconstant
Make the Input to the Recursive Function Variable-Size
Assign Output Variable Before the Recursive Call
Avoid Duplicate Functions in Generated Code
Issue
Cause
Solution
Fixed-Point Conversion
Detect Unexecuted and Constant-Folded Code
What Is Unexecuted Code?
Detect Unexecuted Code
Fix Unexecuted Code
Convert MATLAB Code to Fixed-Point C Code
Propose Fixed-Point Data Types Based on Simulation Ranges
Propose Fixed-Point Data Types Based on Derived Ranges
Specify Type Proposal Options
Detect Overflows
Replace the exp Function with a Lookup Table
Replace a Custom Function with a Lookup Table
Enable Plotting Using the Simulation Data Inspector
Visualize Differences Between Floating-Point and Fixed-Point Results
View and Modify Variable Information
View Variable Information
Modify Variable Information
Revert Changes
Promote Sim Min and Sim Max Values
Automated Fixed-Point Conversion
Automated Fixed-Point Conversion Capabilities
Code Coverage
Proposing Data Types
Locking Proposed Data Types
Viewing Functions
Viewing Variables
Log Data for Histogram
Function Replacements
Validating Types
Testing Numerics
Detecting Overflows
Convert Fixed-Point Conversion Project to MATLAB Scripts
Generated Fixed-Point Code
Location of Generated Fixed-Point Files
Minimizing fi-casts to Improve Code Readability
Avoiding Overflows in the Generated Fixed-Point Code
Controlling Bit Growth
Avoiding Loss of Range or Precision
Handling Non-Constant mpower Exponents
Fixed-Point Code for MATLAB Classes
Automated Conversion Support for MATLAB Classes
Unsupported Constructs
Coding Style Best Practices
Automated Fixed-Point Conversion Best Practices
Create a Test File
Prepare Your Algorithm for Code Acceleration or Code Generation
Check for Fixed-Point Support for Functions Used in Your Algorithm
Manage Data Types and Control Bit Growth
Convert to Fixed Point
Use the Histogram to Fine-Tune Data Type Settings
Optimize Your Algorithm
Avoid Explicit Double and Single Casts
Replacing Functions Using Lookup Table Approximations
MATLAB Language Features Supported for Automated Fixed-Point Conversion
MATLAB Language Features Supported for Automated Fixed-Point Conversion
MATLAB Language Features Not Supported for Automated Fixed-Point Conversion
Inspecting Data Using the Simulation Data Inspector
What Is the Simulation Data Inspector?
Import Logged Data
Export Logged Data
Group Signals
Run Options
Create Report
Comparison Options
Enabling Plotting Using the Simulation Data Inspector
Save and Load Simulation Data Inspector Sessions
Custom Plot Functions
Data Type Issues in Generated Code
Enable the Highlight Option in the MATLAB Coder App
Enable the Highlight Option at the Command Line
Stowaway Doubles
Stowaway Singles
Expensive Fixed-Point Operations
Automated Fixed-Point Conversion Using Programmatic Workflow
Convert MATLAB Code to Fixed-Point C Code
Propose Fixed-Point Data Types Based on Simulation Ranges
Propose Fixed-Point Data Types Based on Derived Ranges
Detect Overflows
Replace the exp Function with a Lookup Table
Replace a Custom Function with a Lookup Table
Enable Plotting Using the Simulation Data Inspector
Visualize Differences Between Floating-Point and Fixed-Point Results
Single-Precision Conversion
Generate Single-Precision C Code at the Command Line
Prerequisites
Create a Folder and Copy Relevant Files
Determine the Type of the Input Argument
Generate and Run Single-Precision MEX to Verify Numerical Behavior
Generate Single-Precision C Code
View the Generated Single-Precision C Code
View Potential Data Type Issues
Generate Single-Precision C Code Using the MATLAB Coder App
Prerequisites
Create a Folder and Copy Relevant Files
Open the MATLAB Coder App
Select the Source Files
Enable Single-Precision Conversion
Define Input Types
Check for Run-Time Issues
Generate Single-Precision C Code
View the Generated C Code
View Potential Data Type Issues
Generate Single-Precision MATLAB Code
Prerequisites
Create a Folder and Copy Relevant Files
Set Up the Single-Precision Configuration Object
Generate Single-Precision MATLAB Code
View the Type Proposal Report
View Generated Single-Precision MATLAB Code
View Potential Data Type Issues
Compare the Double-Precision and Single-Precision Variables
Optionally Generate Single-Precision C Code
Choose a Single-Precision Conversion Workflow
Single-Precision Conversion Best Practices
Use Integers for Index Variables
Limit Use of assert Statements
Initialize MATLAB Class Properties in Constructor
Provide a Test File That Calls Your MATLAB Function
Prepare Your Code for Code Generation
Verify Double-Precision Code Before Single-Precision Conversion
Best Practices for Generation of Single-Precision C/C++ Code
Best Practices for Generation of Single-Precision MATLAB Code
Warnings from Conversion to Single-Precision C/C++ Code
Function Uses Double-Precision in the C89/C90 Standard
Built-In Function Is Implemented in Double-Precision
Built-In Function Returns Double-Precision
Combining Integers and Double-Precision Numbers
MATLAB Language Features Supported for Single-Precision Conversion
MATLAB Language Features Supported for Single-Precision Conversion
MATLAB Language Features Not Supported for Single-Precision Conversion
Setting Up a MATLAB Coder Project
Set Up a MATLAB Coder Project
Create a Project
Open an Existing Project
Specify Properties of Entry-Point Function Inputs Using the App
Why Specify Input Properties?
Specify an Input Definition Using the App
Automatically Define Input Types by Using the App
Make Dimensions Variable-Size When They Meet Size Threshold
Define Input Parameter by Example by Using the App
Define an Input Parameter by Example
Specify Input Parameters by Example
Specify a String Scalar Input Parameter by Example
Specify a Structure Type Input Parameter by Example
Specify a Cell Array Type Input Parameter by Example
Specify an Enumerated Type Input Parameter by Example
Specify an Object Input Type Parameter by Example
Specify a Fixed-Point Input Parameter by Example
Specify an Input from an Entry-Point Function Output Type
Define or Edit Input Parameter Type by Using the App
Define or Edit an Input Parameter Type
Specify a String Scalar Input Parameter
Specify an Enumerated Type Input Parameter
Specify a Fixed-Point Input Parameter
Specify a Structure Input Parameter
Specify a Cell Array Input Parameter
Define Constant Input Parameters Using the App
Define Inputs Programmatically in the MATLAB File
Add Global Variables by Using the App
Specify Global Variable Type and Initial Value Using the App
Why Specify a Type Definition for Global Variables?
Specify a Global Variable Type
Define a Global Variable by Example
Define or Edit Global Variable Type
Define Global Variable Initial Value
Define Global Variable Constant Value
Remove Global Variables
Undo and Redo Changes to Type Definitions in the App
Code Generation Readiness Screening in the MATLAB Coder App
Slow Operations in MATLAB Coder App
Unable to Open a MATLAB Coder Project
Preparing MATLAB Code for C/C++ Code Generation
Workflow for Preparing MATLAB Code for Code Generation
See Also
Fixing Errors Detected at Design Time
See Also
Using the Code Analyzer
Check Code with the Code Analyzer
Check Code by Using the Code Generation Readiness Tool
Run Code Generation Readiness Tool at the Command Line
Run Code Generation Readiness Tool from the Current Folder Browser
Run the Code Generation Readiness Tool Using the MATLAB Coder App
Code Generation Readiness Tool
Issues Tab
Files Tab
Unable to Determine Code Generation Readiness
Generate MEX Functions by Using the MATLAB Coder App
Workflow for Generating MEX Functions Using the MATLAB Coder App
Generate a MEX Function Using the MATLAB Coder App
Configure Project Settings
Build a MATLAB Coder Project
See Also
Generate MEX Functions at the Command Line
Command-line Workflow for Generating MEX Functions
Generate a MEX Function at the Command Line
Fix Errors Detected at Code Generation Time
See Also
Running and Debugging MEX Functions
Debug MEX Functions
Debug MEX Functions by Using a C/C++ Debugger
Debugging Strategies
Collect and View Line Execution Counts for Your MATLAB Code
Resolve Error: Function Is Not Supported for Code Generation
Issue
Possible Solutions
Debug Generated C/C++ Code
Testing MEX Functions in MATLAB
Why Test MEX Functions in MATLAB?
Workflow for Testing MEX Functions in MATLAB
See Also
Running MEX Functions
Debug MEX Functions
Debug MEX Functions by Using a C/C++ Debugger
Check for Run-Time Issues by Using the App
Collect MATLAB Line Execution Counts
Disable JIT Compilation for Parallel Loops
Verify MEX Functions in the MATLAB Coder App
Verify MEX Functions at the Command Line
Debug Run-Time Errors
Viewing Errors in the Run-Time Stack
Handling Run-Time Errors
Using MEX Functions That MATLAB Coder Generates
Generating C/C++ Code from MATLAB Code
Code Generation Workflow
See Also
Generating Standalone C/C++ Executables from MATLAB Code
Generate a C Executable Using the MATLAB Coder App
Generate a C Executable at the Command Line
Specifying main Functions for C/C++ Executables
Specify main Functions
Configure Build Settings
Specify Build Type
Specify a Language for Code Generation
Specify Output File Name
Specify Output File Locations
Parameter Specification Methods
Specify Build Configuration Parameters
Specify Configuration Parameters in Command-Line Workflow Interactively
Create and Modify Configuration Objects by Using the Dialog Box
Additional Functionalities in the Dialog Box
Specify Data Types Used in Generated Code
Specify Data Type Using the MATLAB Coder App
Specify Data Type at the Command Line
Use Generated Initialize and Terminate Functions
Initialize Function
Terminate Function
Change the Language Standard
Convert codegen Command to Equivalent MATLAB Coder Project
Example: Convert a Complete codegen Command to a Project File
Example: Convert an Incomplete codegen Command to a Template Project File
Limitations
Share Build Configuration Settings
Export Settings
Import Settings
Convert MATLAB Coder Project to MATLAB Script
Convert a Project Using the MATLAB Coder App
Convert a Project Using the Command-Line Interface
Run the Script
Special Cases That Generate Additional MAT-File
Preserve Variable Names in Generated Code
Reserved Keywords
C Reserved Keywords
C++ Reserved Keywords
Keywords Reserved for Code Generation
Reserved Prefixes
MATLAB Coder Code Replacement Library Keywords
Specify Properties of Entry-Point Function Inputs
Why You Must Specify Input Properties
Properties to Specify
Rules for Specifying Properties of Primary Inputs
Methods for Defining Properties of Primary Inputs
Define Input Properties by Example at the Command Line
Specify Constant Inputs at the Command Line
Specify Variable-Size Inputs at the Command Line
Input Type Specification and arguments blocks
Specify Cell Array Inputs at the Command Line
Specify Cell Array Inputs by Example
Specify the Type of the Cell Array Input
Make a Homogeneous Copy of a Type
Make a Heterogeneous Copy of a Type
Specify Variable-Size Cell Array Inputs
Specify Type Name for Heterogeneous Cell Array Inputs
Specify Constant Cell Array Inputs
Constant Input Checking in MEX Functions
Control Whether a MEX Function Checks the Value of a Constant Input
Define Input Properties Programmatically in the MATLAB File
How to Use assert with MATLAB Coder
Rules for Using assert Function
Specifying General Properties of Primary Inputs
Specifying Properties of Primary Fixed-Point Inputs
Specifying Properties of Cell Arrays
Specifying Class and Size of Scalar Structure
Specifying Class and Size of Structure Array
Create and Edit Input Types by Using the Coder Type Editor
Open the Coder Type Editor
Common Editor Actions
Type Browser Pane
Type Properties Pane
MATLAB Code Pane
Speed Up Compilation by Generating Only Code
Disable Creation of the Code Generation Report
Paths and File Infrastructure Setup
Compile Path Search Order
Specify Folders to Search for Custom Code
Naming Conventions
Generate Code for Multiple Entry-Point Functions
Generating Code for Multiple Entry-Point Functions
Call a Single Entry-Point Function from a MEX Function
Generate Code for More Than One Entry-Point Function Using the MATLAB Coder App
Generate One MEX Function for Multiple Signatures
Generate Multisignature MEX Function for a Single Entry-Point Function
Generate Multisignature MEX Function for Multiple Entry-Point Functions
Pass an Entry-Point Function Output as an Input
Pass an Entry-Point Function Output as an Input to Another Entry-Point Function
Use coder.OutputType to Facilitate Code Componentization
Generate Code for Global Data
Workflow
Declare Global Variables
Define Global Data
Synchronizing Global Data with MATLAB
Define Constant Global Data
Global Data Limitations for Generated Code
Specify Global Cell Arrays at the Command Line
Generate Code for Enumerations
Generate Code for Variable-Size Data
Disable Support for Variable-Size Data
Control Dynamic Memory Allocation
Generating Code for MATLAB Functions with Variable-Size Data
Generate Code for a MATLAB Function That Expands a Vector in a Loop
How MATLAB Coder Partitions Generated Code
Partitioning Generated Files
How to Select the File Partitioning Method
Partitioning Generated Files with One C/C++ File Per MATLAB File
Generated Files and Locations
File Partitioning and Inlining
Requirements for Signed Integer Representation
Build Process Customization
RTW.BuildInfo Methods
coder.updateBuildInfo Function
coder.ExternalDependency Class
Post-Code-Generation Command
Run-time Stack Overflow
Compiler and Linker Errors
Failure to Specify a Main Function
Failure to Specify External Code Files
Errors Caused by External Code
Pass Structure Arguments by Reference or by Value in Generated Code
Name the C Structure Type to Use With a Global Structure Variable
Generate Code for an LED Control Function That Uses Enumerated Types
Generate Code That Uses N-Dimensional Indexing
Improve Readability with N-Dimensional Indexing and Row-Major Layout
Column-Major Layout and N-Dimensional Indexing
Other Code Generation Considerations
Install OpenMP Library on macOS Platform
Generate Code to Detect Edges on Images
C Code Generation for a MATLAB Kalman Filtering Algorithm
Generate Code to Optimize Portfolio by Using Black Litterman Approach
Generate Code for Persistent Variables
Generate Code for Structure Arrays
Add Custom Toolchains to MATLAB® Coder™ Build Process
Generate Code for Sobel Edge Detection That Uses Half-Precision Data Type
Half Precision in MATLAB
Half Precision Code Generation Support
Generate Native Half-Precision C Code Using MATLAB Coder
Generate Native Half-Precision C Code for ARM® Cortex®-A with GCC Compiler
Generate Native Half-Precision C Code for ARM Cortex-A with Armclang Compiler
Register ARM Target Hardware with Custom Language Implementation
What is Half Precision?
Half Precision Applications
Benefits of Using Half Precision in Embedded Applications
Half Precision in MATLAB
Half Precision in Simulink
Code Generation with Half Precision
Build Process Support for File and Folder Names
Filenames with Spaces
Folder Names with Spaces
Troubleshooting Errors When Folder Names Have Spaces
Folder Names with Special Characters
Very Long Folder Paths
Generate Code That Reads Data from a File
Verify Generated C/C++ Code
Tracing Generated C/C++ Code to MATLAB Source Code
Generate Traceability Tags
Format of Traceability Tags
Location of Comments in Generated Code
Traceability Tag Limitations
Code Generation Reports
Report Generation
Report Location
Errors and Warnings
Files and Functions
MATLAB Source
MATLAB Variables
Tracing Code
Code Insights
Additional Reports
Report Limitations
Access Code Generation Report Information Programmatically
Create Report Information Object
Example: Create Report Information Object for Successful Code Generation
Example: Create Report Information Object for Successful Code Generation That Checks Out Toolbox Licenses
Example: Create Report Information Object for Failed Code Generation
Inspect Code Manually
Transferring Code Configuration Objects to a New MATLAB Session
Generate Standalone C/C++ Code That Detects and Reports Run-Time Errors
Generated C Code vs. Generated C++ Code
Example: Compare Generated C and C++ Code That Include Run-Time Checks
Limitations
Example: Generate Standalone C Code That Detects and Reports Run-Time Errors
Testing Code Generated from MATLAB Code
Unit Test Generated Code with MATLAB Coder
Unit Test External C Code with MATLAB Coder
Calculate Number of Lines of Code by Using Report Information Object
Code Replacement for MATLAB Code
What Is Code Replacement?
Code Replacement Libraries
Code Replacement Terminology
Code Replacement Limitations
Choose a Code Replacement Library
About Choosing a Code Replacement Library
Explore Available Code Replacement Libraries
Explore Code Replacement Library Contents
Replace Code Generated from MATLAB Code
Generate SIMD Code for MATLAB Functions
MATLAB Functions That Support SIMD Code
Generate SIMD Code Versus Plain C Code
Limitations
Custom Toolchain Registration
Custom Toolchain Registration
What Is a Custom Toolchain?
What Is a Factory Toolchain?
What is a Toolchain Definition?
Key Terms
Typical Workflow
About coder.make.ToolchainInfo
Create and Edit Toolchain Definition File
Toolchain Definition File with Commentary
Steps Involved in Writing a Toolchain Definition File
Write a Function That Creates a ToolchainInfo Object
Setup
Macros
C Compiler
C++ Compiler
Linker
Archiver
Builder
Build Configurations
Create and Validate ToolchainInfo Object
Register the Custom Toolchain
Use the Custom Toolchain
Troubleshooting Custom Toolchain Validation
Build Tool Command Path Incorrect
Build Tool Not in System Path
Tool Path Does Not Exist
Path Incompatible with Builder or Build Tool
Unsupported Platform
Toolchain is Not installed
Project or Configuration Is Using the Template Makefile
Prevent Circular Data Dependencies with One-Pass or Single-Pass Linkers
Build 32-bit DLL on 64-bit Windows® Platform Using MSVC Toolchain
Deploying Generated Code
C Compiler Considerations for Signed Integer Overflows
Use C Arrays in the Generated Function Interfaces
Implementation of Arrays in the Generated C/C++ Code
The emxArray Dynamic Data Structure Definition
Utility Functions for Interacting with emxArray Data
Examples
Use Dynamically Allocated C++ Arrays in Generated Function Interfaces
Using the coder::array Class Template
Examples
Change Interface Generation
Use a Dynamic Library in a Microsoft Visual Studio Project
Incorporate Generated Code Using an Example Main Function
Workflow for Using an Example Main Function
Control Example Main Generation Using the MATLAB Coder App
Control Example Main Generation Using the Command-Line Interface
Use an Example C Main in an Application
Prerequisites
Create a Folder and Copy Relevant Files
Run the Sobel Filter on the Image
Generate and Test a MEX Function
Generate an Example Main Function for sobel.m
Copy the Example Main Files
Modify the Generated Example Main Function
Generate the Sobel Filter Application
Run the Sobel Filter Application
Display the Resulting Image
Package Code for Other Development Environments
When to Package Code
Package Generated Code Using the MATLAB Coder App
Package Generated Code at the Command Line
Specify packNGo Options
Structure of Generated Example C/C++ Main Function
Contents of the File main.c or main.cpp
Contents of the File main.h
Troubleshoot Failures in Deployed Code
Using Dynamic Memory Allocation for an Atoms Simulation
Register New Hardware Devices
Specify Hardware Implementation for New Device
Specify Hardware Implementation That Persists Over MATLAB Sessions
Create Hardware Implementation by Modifying Existing Implementation
Create Hardware Implementation by Reusing Existing Implementation
Validate Hardware Device Data
Export Hardware Device Data
Create Alternative Identifier for Target Object
Upgrade Data Definitions for Hardware Devices
Configure CMake Build Process
Specify CMake Toolchain Definition
Available CMake Toolchain Definitions
Create Custom CMake Toolchain Definition
Deploy Generated C Code to External Hardware: Raspberry Pi Examples
Prerequisites
Hardware Implementation Parameters
Hello World Example
Spring Mass Damper System Example
Deploy Generated Code
Main Function
Generated Function Interfaces
Executable Applications
Static and Dynamic Libraries
Generated File Structure
Code Verification
Custom Hardware Considerations
Other Deployment Strategies
Approaches for Building Code Generated from MATLAB Code
Accelerating MATLAB Algorithms
Workflow for Accelerating MATLAB Algorithms
See Also
Best Practices for Using MEX Functions to Accelerate MATLAB Algorithms
Accelerate Code That Dominates Execution Time
Include Loops Inside MEX Function
Avoid Generating MEX Functions from Unsupported Functions
Avoid Generating MEX Functions if Built-In MATLAB Functions Dominate Run Time
Minimize MEX Function Calls
Accelerate MATLAB Algorithms
Modifying MATLAB Code for Acceleration
How to Modify Your MATLAB Code for Acceleration
Profile MEX Functions by Using MATLAB Profiler
MEX Profile Generation
Example
Effect of Folding Expressions on MEX Code Coverage
Control Run-Time Checks
Types of Run-Time Checks
When to Disable Run-Time Checks
How to Disable Run-Time Checks
Algorithm Acceleration Using Parallel for-Loops (parfor)
Parallel for-Loops (parfor) in Generated Code
How parfor-Loops Improve Execution Speed
When to Use parfor-Loops
When Not to Use parfor-Loops
parfor-Loop Syntax
parfor Restrictions
Control Compilation of parfor-Loops
When to Disable parfor
Reduction Assignments in parfor-Loops
What are Reduction Assignments?
Multiple Reductions in a parfor-Loop
Classification of Variables in parfor-Loops
Overview
Sliced Variables
Broadcast Variables
Reduction Variables
Temporary Variables
Accelerate MATLAB Algorithms That Use Parallel for-Loops (parfor)
Specify Maximum Number of Threads in parfor-Loops
Troubleshooting parfor-Loops
Global or Persistent Declarations in parfor-Loop
Compiler Does Not Support OpenMP
Generate MEX Code to Accelerate Simulation of Bouncing Balls
Generate MEX Code to Calculate Geodesics in Curved Space-Time
Generate Accelerated MEX Code for Reverberation Using MATLAB Classes
Using PARFOR to Speed Up an Image Contrast Enhancement Algorithm
Use Generated Code to Accelerate an Application Deployed with MATLAB Compiler
External Code Integration
Call Custom C/C++ Code from the Generated Code
Call C Code
Return Multiple Values from a C Function
Pass Data by Reference
Integrate External Code that Uses Custom Data Types
Integrate External Code that Uses Pointers, Structures, and Arrays
Configure Build for External C/C++ Code
Provide External Files for Code Generation
Configure Build from Within a Function
Configure Build by Using the Configuration Object
Configure Build by Using the MATLAB Coder App
Develop Interface for External C/C++ Code
Create a class from coder.ExternalDependency
Best Practices for Using coder.ExternalDependency
Mapping MATLAB Types to Types in Generated Code
Complex Types
Structure Types
Fixed-Point Types
Character Vectors
Multiword Types
Generate Code to Read a Text File
Generate C/C++ Strings from MATLAB Strings and Character Row Vectors
Add New Line to Strings in Generated Code
Limitations
Generate Efficient and Reusable Code
Optimization Strategies
Modularize MATLAB Code
Avoid Data Copies of Function Inputs in Generated Code
Inline Code
Control Inlining to Fine-Tune Performance and Readability of Generated Code
Control Inlining of a Specific MATLAB Function
Control Inlining by Using Code Generation Settings
Interaction Between Different Inlining Controls
Example: Control Inlining at the Boundary Between Your Functions and MathWorks® Functions
Fold Function Calls into Constants
Control Stack Space Usage
Stack Allocation and Performance
Allocate Heap Space from Command Line
Allocate Heap Space Using the MATLAB Coder App
Dynamic Memory Allocation and Performance
When Dynamic Memory Allocation Occurs
Minimize Dynamic Memory Allocation
Provide Maximum Size for Variable-Size Arrays
Disable Dynamic Memory Allocation During Code Generation
Set Dynamic Memory Allocation Threshold
Set Dynamic Memory Allocation Threshold Using the MATLAB Coder App
Set Dynamic Memory Allocation Threshold at the Command Line
Optimize Dynamic Array Access
Disable Cache Dynamic Array Data Pointer Property
Compare Generated C Code
Excluding Unused Paths from Generated Code
Prevent Code Generation for Unused Execution Paths
Prevent Code Generation When Local Variable Controls Flow
Prevent Code Generation When Input Variable Controls Flow
Generate Code with Parallel for-Loops (parfor)
Minimize Redundant Operations in Loops
Unroll for-Loops and parfor-Loops
Force for-Loop Unrolling by Using coder.unroll
Set Loop Unrolling Threshold for All for-Loops and parfor-Loops in the MATLAB Code
Disable Support for Integer Overflow or Nonfinites
Disable Support for Integer Overflow
Disable Support for Nonfinite Numbers
Integrate External/Custom Code
MATLAB Coder Optimizations in Generated Code
Constant Folding
Loop Fusion
Successive Matrix Operations Combined
Unreachable Code Elimination
memcpy Calls
memset Calls
Use coder.const with Extrinsic Function Calls
Reduce Code Generation Time by Using coder.const with feval
Force Constant-Folding by Using coder.const with feval
memcpy Optimization
memset Optimization
Reuse Large Arrays and Structures
LAPACK Calls in Generated Code
Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls
Specify LAPACK Library
Write LAPACK Callback Class
Generate LAPACK Calls by Specifying a LAPACK Callback Class
Locate LAPACK Library in Execution Environment
BLAS Calls in Generated Code
Speed Up Matrix Operations in Generated Standalone Code by Using BLAS Calls
Specify BLAS Library
Write BLAS Callback Class
Generate BLAS Calls by Specifying a BLAS Callback Class
Locate BLAS Library in Execution Environment
Usage Notes and Limitations for OpenBLAS Library
Speed Up Fast Fourier Transforms in Generated Standalone Code by Using FFTW Library Calls
FFTW Planning Considerations
Install FFTW Library
Write an FFT Callback Class
Generate FFTW Library Calls by Specifying an FFT Library Callback Class
Synchronize Multithreaded Access to FFTW Planning in Generated Standalone Code
Prerequisites
Create a MATLAB Function
Write Supporting C Code
Write an FFT Library Callback Class
Generate a Dynamically Linked Library
Specify Configuration Parameters in the MATLAB Coder App
Speed Up MEX Generation by Using JIT Compilation
Specify Use of JIT Compilation in the MATLAB Coder App
Specify Use of JIT Compilation at the Command Line
JIT Compilation Incompatibilities
Automatically Parallelize for Loops in Generated Code
Parallelize for Loops by Using MATLAB Coder App
Parallelize for Loops at Command Line
Inspect Generated Code and Code Insights
Disable Automatic Parallelization of a for Loop
Parallelize Implicit for Loops
Parallelize for Loops Performing Reduction Operations
Usage Notes and Limitations
Specify Maximum Number of Threads to Run Parallel for-Loops in the Generated Code
Specify Number of Threads by Using MATLAB Coder App
Specify Number of Threads at the Command Line
Create Custom Hardware Processor
Optimize Generated Code for Fast Fourier Transform Functions
Intel Target Support
ARM Target Support
MEX Target Support
Generating Reentrant C Code from MATLAB Code
Generate Reentrant C Code from MATLAB Code
About This Tutorial
Copying Files Locally
About the Example
Providing a C main Function
Configuring Build Parameters
Generating the C Code
Viewing the Generated C Code
Running the Code
Key Points to Remember
Learn More
Reentrant Code
Specify Generation of Reentrant Code
Specify Generation of Reentrant Code Using the MATLAB Coder App
Specify Generation of Reentrant Code Using the Command-Line Interface
API for Generated Reusable Code
Call Reentrant Code in a Single-Threaded Environment
Call Reentrant Code in a Multithreaded Environment
Multithreaded Examples
Call Reentrant Code with No Persistent or Global Data (UNIX Only)
Provide a Main Function
Generate Reentrant C Code
Examine the Generated Code
Run the Code
Call Reentrant Code — Multithreaded with Persistent Data (Windows Only)
MATLAB Code for This Example
Provide a Main Function
Generate Reentrant C Code
Examine the Generated Code
Run the Code
Call Reentrant Code — Multithreaded with Persistent Data (UNIX Only)
MATLAB Code for This Example
Provide a Main Function
Generate Reentrant C Code
Examine the Generated Code
Run the Code
Troubleshooting Code Generation Problems
JIT MEX Incompatibility Warning
Issue
Cause
Solution
JIT Compilation Does Not Support OpenMP
Issue
Cause
Solution
Output Variable Must Be Assigned Before Run-Time Recursive Call
Issue
Cause
Solution
Compile-Time Recursion Limit Reached
Issue
Cause
Solutions
Force Run-Time Recursion
Increase the Compile-Time Recursion Limit
Unable to Determine That Every Element of Cell Array Is Assigned
Issue
Cause
Solution
Nonconstant Index into varargin or varargout in a for-Loop
Issue
Cause
Solution
Unknown Output Type for coder.ceval
Issue
Cause
Solution
MEX Generated on macOS Platform Stays Loaded in Memory
Issue
Cause
Solution
Resolve Error: Code Generator Failed to Produce C++ Destructor for MATLAB Class
Issue
Possible Solutions
Row-Major Array Layout
Row-Major and Column-Major Array Layouts
Array Storage in Computer Memory
Conversions Between Different Array Layouts
Generate Code That Uses Row-Major Array Layout
Specify Row-Major Layout
Array Layout and Algorithmic Efficiency
Row-Major Layout for N-Dimensional Arrays
Specify Array Layout in External Function Calls
Deep Learning with MATLAB Coder
Prerequisites for Deep Learning with MATLAB Coder
MathWorks Products
Third-Party Hardware and Software
Environment Variables
Workflow for Deep Learning Code Generation with MATLAB Coder
Networks and Layers Supported for Code Generation
Supported Pretrained Networks
Supported Layers
Supported Classes
int8 Code Generation
Analyze Network for Code Generation
Check dlnetwork for Code Generation Compatibility
Analyze Classification Network for Code Generation Compatibility
Load Pretrained Networks for Code Generation
Load a Network by Using coder.loadDeepLearningNetwork
Specify a Network Object for Code Generation
Specify a dlnetwork Object for Code Generation
Generate Generic C/C++ Code for Deep Learning Networks
Requirements
Code Generation by Using codegen
Code Generation by Using the MATLAB Coder App
Code Generation for Deep Learning Networks with MKL-DNN
Requirements
Code Generation by Using codegen
Code Generation by Using the MATLAB Coder App
Code Generation for Deep Learning Networks with ARM Compute Library
Requirements
Code Generation by Using codegen
Code Generation by Using the MATLAB Coder App
Cross-Compile Deep Learning Code That Uses ARM Compute Library
Prerequisites
Generate and Deploy Deep Learning Code
Generate int8 Code for Deep Learning Networks
ARM Cortex-A Processors
ARM Cortex-M Processors
Update Network Parameters After Code Generation
Create an Entry-Point Function
Create a Network
Code Generation by Using codegen
Run the Generated MEX
Update Network with Different Learnable Parameters
Run the Generated MEX with Updated Learnables
Limitations
Deep Learning Code Generation on Intel Targets for Different Batch Sizes
Deep Learning Prediction with ARM Compute Using codegen
Code Generation for Deep Learning on ARM Targets
Generate C++ Code for Object Detection Using YOLO v2 and Intel MKL-DNN
Code Generation and Deployment of MobileNet-v2 Network to Raspberry Pi
Code Generation for Semantic Segmentation Application on Intel CPUs That Uses U-Net
Code Generation for Semantic Segmentation Application on ARM Neon Targets That Uses U-Net
Code Generation for LSTM Network on Raspberry Pi
Code Generation for LSTM Network That Uses Intel MKL-DNN
Code Generation for Convolutional LSTM Network That Uses Intel MKL-DNN
Cross Compile Deep Learning Code for ARM Neon Targets
Generate INT8 Code for Deep Learning Network on Raspberry Pi
Generate INT8 Code for Deep Learning Network on Cortex-M Target
Generate Generic C/C++ Code for Sequence-to-Sequence Regression That Uses Deep Learning
Generate Digit Images Using Variational Autoencoder on Intel CPUs
Post-Code-Generation Update of Deep Learning Network Parameters
Generate Code for LSTM Network and Deploy on Cortex-M Target
Prune Filters in a Detection Network Using Taylor Scores
Generating Code for C++
C++ Code Generation
Generate C++ Code
C++ Language Features Supported in Generated Code
Additional Differences Between Generated C Code and C++ Code
Generate C++ Code with Class Interface
Generate C++ Code with a Class Interface
Globals and Persistents in a Generated C++ Class
Put Multiple Entry-Point Functions in the Same Class
Organize Generated C++ Code into Namespaces
Settings That Control Namespace Structure
Example: Generate C++ Code with Namespaces
Integrate Multiple Generated C++ Code Projects
Generate C++ Classes for MATLAB Classes That Model Simple and Damped Oscillators
Simulation Data Inspector
View Data in the Simulation Data Inspector
View Logged Data
Import Data from the Workspace or a File
View Complex Data
View String Data
View Frame-Based Data
View Event-Based Data
Import Data from a CSV File into the Simulation Data Inspector
Basic File Format
Multiple Time Vectors
Signal Metadata
Import Data from a CSV File
Microsoft Excel Import, Export, and Logging Format
Basic File Format
Multiple Time Vectors
Signal Metadata
User-Defined Data Types
Complex, Multidimensional, and Bus Signals
Function-Call Signals
Simulation Parameters
Multiple Runs
Configure the Simulation Data Inspector
Logged Data Size and Location
Archive Behavior and Run Limit
Incoming Run Names and Location
Signal Metadata to Display
Signal Selection on the Inspect Pane
How Signals Are Aligned for Comparison
Colors Used to Display Comparison Results
Signal Grouping
Data to Stream from Parallel Simulations
Options for Saving and Loading Session Files
Signal Display Units
How the Simulation Data Inspector Compares Data
Signal Alignment
Synchronization
Interpolation
Tolerance Specification
Limitations
Save and Share Simulation Data Inspector Data and Views
Save and Load Simulation Data Inspector Sessions
Share Simulation Data Inspector Views
Share Simulation Data Inspector Plots
Create Simulation Data Inspector Report
Export Data to the Workspace or a File
Export Video Signal to an MP4 File
Inspect and Compare Data Programmatically
Create a Run and View the Data
Compare Two Signals in the Same Run
Compare Runs with Global Tolerance
Analyze Simulation Data Using Signal Tolerances
Limit the Size of Logged Data
Limit the Number of Runs Retained in the Simulation Data Inspector Archive
Specify a Minimum Disk Space Requirement or Maximum Size for Logged Data
View Data Only During Simulation
Reduce the Number of Data Points Logged from Simulation
📜 SIMILAR VOLUMES