ebook img

MATLAB® Coder User's Guide PDF

1476 Pages·2022·12.252 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview MATLAB® Coder User's Guide

MATLAB® Coder™ User's Guide R2022b How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 1 Apple Hill Drive Natick, MA 01760-2098 MATLAB® Coder™ User's Guide © COPYRIGHT 2011–2022 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History April 2011 Online only New for Version 2 (R2011a) September 2011 Online only Revised for Version 2.1 (Release 2011b) March 2012 Online only Revised for Version 2.2 (Release 2012a) September 2012 Online only Revised for Version 2.3 (Release 2012b) March 2013 Online only Revised for Version 2.4 (Release 2013a) September 2013 Online only Revised for Version 2.5 (Release 2013b) March 2014 Online only Revised for Version 2.6 (Release 2014a) October 2014 Online only Revised for Version 2.7 (Release 2014b) March 2015 Online only Revised for Version 2.8 (Release 2015a) September 2015 Online only Revised for Version 3.0 (Release 2015b) October 2015 Online only Rereleased for Version 2.8.1 (Release 2015aSP1) March 2016 Online only Revised for Version 3.1 (Release 2016a) September 2016 Online only Revised for Version 3.2 (Release 2016b) March 2017 Online only Revised for Version 3.3 (Release 2017a) September 2017 Online only Revised for Version 3.4 (Release 2017b) March 2018 Online only Revised for Version 4.0 (Release 2018a) September 2018 Online only Revised for Version 4.1 (Release 2018b) March 2019 Online only Revised for Version 4.2 (Release 2019a) September 2019 Online only Revised for Version 4.3 (Release 2019b) March 2020 Online only Revised for Version 5.0 (Release 2020a) September 2020 Online only Revised for Version 5.1 (Release 2020b) March 2021 Online only Revised for Version 5.2 (Release 2021a) September 2021 Online only Revised for Version 5.3 (Release 2021b) March 2022 Online only Revised for Version 5.4 (Release R2022a) September 2022 Online only Revised for Version 5.5 (Release R2022b) Check Bug Reports for Issues and Fixes Software is inherently complex and is not free of errors. The output of a code generator might contain bugs, some of which are not detected by a compiler. MathWorks reports critical known bugs brought to its attention on its Bug Report system at www.mathworks.com/support/bugreports/. In the search bar, type the phrase "Incorrect Code Generation" to obtain a report of known bugs that produce code that might compile and execute, but still produce wrong answers. To save a search, click Save Search. The bug reports are an integral part of the documentation for each release. Examine periodically all bug reports for a release, as such reports may identify inconsistencies between the actual behavior of a release you are using and the behavior described in this documentation. In addition to reviewing bug reports, you should implement a verification and validation strategy to identify potential bugs in your design, code, and tools. Contents About MATLAB Coder 1 MATLAB Coder Product Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Product Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 When to Use MATLAB Coder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 Code Generation for Embedded Software Applications . . . . . . . . . . . . . . . 1-3 Code Generation for Fixed-Point Algorithms . . . . . . . . . . . . . . . . . . . . . . . 1-3 Design Considerations for C/C++ Code Generation 2 When to Generate Code from MATLAB Algorithms . . . . . . . . . . . . . . . . . . 2-2 When Not to Generate Code from MATLAB Algorithms . . . . . . . . . . . . . . . 2-2 Which Code Generation Feature to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Prerequisites for C/C++ Code Generation from MATLAB . . . . . . . . . . . . . 2-4 MATLAB Code Design Considerations for Code Generation . . . . . . . . . . . 2-5 See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Differences Between Generated Code and MATLAB Code . . . . . . . . . . . . . 2-6 Functions that have Multiple Possible Outputs . . . . . . . . . . . . . . . . . . . . . 2-7 Writing to ans Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Logical Short-Circuiting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Loop Index Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Indexing for Loops by Using Single Precision Operands . . . . . . . . . . . . . . 2-9 Index of an Unentered for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10 Character Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11 Order of Evaluation in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11 Name Resolution While Constructing Function Handles . . . . . . . . . . . . . 2-12 Termination Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Size of Variable-Size N-D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Size of Empty Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Size of Empty Array That Results from Deleting Elements of an Array . . . 2-14 Binary Element-Wise Operations with Single and Double Operands . . . . . 2-14 Floating-Point Numerical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15 NaN and Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15 Negative Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16 Code Generation Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16 MATLAB Class Property Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16 vii MATLAB Classes in Nested Property Assignments That Have Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16 MATLAB Handle Class Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17 Variable-Size Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17 Converting Strings with Consecutive Unary Operators to double . . . . . . 2-17 Display Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17 Potential Differences Reporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20 Addressing Potential Differences Messages . . . . . . . . . . . . . . . . . . . . . . 2-20 Disabling and Enabling Potential Differences Reporting for MATLAB Coder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20 Disabling and Enabling Potential Differences Reporting for Fixed-Point Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21 Potential Differences Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22 Automatic Dimension Incompatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22 mtimes No Dynamic Scalar Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22 Matrix-Matrix Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-23 Vector-Vector Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-23 Loop Index Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-24 MATLAB Language Features Supported for C/C++ Code Generation . . . 2-26 MATLAB Features That Code Generation Supports . . . . . . . . . . . . . . . . . 2-26 MATLAB Language Features That Code Generation Does Not Support . . 2-27 Functions, Classes, and System Objects Supported for Code Generation 3 Functions and Objects Supported for C/C++ Code Generation . . . . . . . . . 3-2 Defining MATLAB Variables for C/C++ Code Generation 4 Variables Definition for Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Best Practices for Defining Variables for C/C++ Code Generation . . . . . . 4-3 Define Variables By Assignment Before Using Them . . . . . . . . . . . . . . . . . 4-3 Use Caution When Reassigning Variables . . . . . . . . . . . . . . . . . . . . . . . . . 4-5 Use Type Cast Operators in Variable Definitions . . . . . . . . . . . . . . . . . . . . 4-5 Define Matrices Before Assigning Indexed Variables . . . . . . . . . . . . . . . . . 4-5 Index Arrays by Using Constant Value Vectors . . . . . . . . . . . . . . . . . . . . . 4-5 Eliminate Redundant Copies of Variables in Generated Code . . . . . . . . . . 4-7 When Redundant Copies Occur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 How to Eliminate Redundant Copies by Defining Uninitialized Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 Defining Uninitialized Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 viii Contents Reassignment of Variable Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9 Reuse the Same Variable with Different Properties . . . . . . . . . . . . . . . . . 4-10 When You Can Reuse the Same Variable with Different Properties . . . . . 4-10 When You Cannot Reuse Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Limitations of Variable Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11 Supported Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Edit and Represent Coder Type Objects and Properties . . . . . . . . . . . . . . 4-14 Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14 Legacy Representation of Coder Type Objects . . . . . . . . . . . . . . . . . . . . 4-15 Defining Data for Code Generation 5 Data Definition Considerations for Code Generation . . . . . . . . . . . . . . . . . 5-2 Code Generation for Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8 Restrictions When Defining Complex Variables . . . . . . . . . . . . . . . . . . . . . 5-8 Code Generation for Complex Data with Zero-Valued Imaginary Parts . . . . 5-8 Results of Expressions That Have Complex Operands . . . . . . . . . . . . . . . 5-11 Results of Complex Multiplication with Nonfinite Values . . . . . . . . . . . . . 5-11 Encoding of Characters in Code Generation . . . . . . . . . . . . . . . . . . . . . . . 5-12 Array Size Restrictions for Code Generation . . . . . . . . . . . . . . . . . . . . . . 5-13 Code Generation for Constants in Structures and Arrays . . . . . . . . . . . . 5-14 Code Generation for Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Differences Between Generated Code and MATLAB Code . . . . . . . . . . . . 5-16 Define String Scalar Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17 Define String Scalar Types at the Command Line . . . . . . . . . . . . . . . . . . 5-17 Define String Scalar Inputs in the MATLAB Coder App . . . . . . . . . . . . . . 5-18 Code Generation for Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19 Sparse Data Types in Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19 Input Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19 Code Generation Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20 Code Generation Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21 Specify Array Layout in Functions and Classes . . . . . . . . . . . . . . . . . . . . . 5-22 Specify Array Layout in a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22 Query Array Layout of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23 Specify Array Layout in a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23 Code Design for Row-Major Array Layout . . . . . . . . . . . . . . . . . . . . . . . . . 5-26 Understand Potential Inefficiencies Caused by Array Layout . . . . . . . . . . 5-26 Linear Indexing Uses Column-Major Array Layout . . . . . . . . . . . . . . . . . 5-28 ix Code Generation for Variable-Size Data 6 Code Generation for Variable-Size Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Memory Allocation for Variable-Size Arrays . . . . . . . . . . . . . . . . . . . . . . . 6-2 Enabling and Disabling Support for Variable-Size Arrays . . . . . . . . . . . . . 6-3 Variable-Size Arrays in a Code Generation Report . . . . . . . . . . . . . . . . . . . 6-3 Control Memory Allocation for Variable-Size Arrays . . . . . . . . . . . . . . . . . 6-4 Provide Upper Bounds for Variable-Size Arrays . . . . . . . . . . . . . . . . . . . . . 6-4 Disable Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4 Configure Code Generator to Use Dynamic Memory Allocation for Arrays Bigger Than a Threshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4 Specify Upper Bounds for Variable-Size Arrays . . . . . . . . . . . . . . . . . . . . . 6-6 Specify Upper Bounds for Variable-Size Inputs . . . . . . . . . . . . . . . . . . . . . 6-6 Specify Upper Bounds for Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . 6-6 Define Variable-Size Data for Code Generation . . . . . . . . . . . . . . . . . . . . . 6-8 Use a Matrix Constructor with Nonconstant Dimensions . . . . . . . . . . . . . . 6-8 Assign Multiple Sizes to the Same Variable . . . . . . . . . . . . . . . . . . . . . . . . 6-8 Define Variable-Size Data Explicitly by Using coder.varsize . . . . . . . . . . . . 6-9 Diagnose and Fix Variable-Size Data Errors . . . . . . . . . . . . . . . . . . . . . . . 6-12 Diagnosing and Fixing Size Mismatch Errors . . . . . . . . . . . . . . . . . . . . . 6-12 Diagnosing and Fixing Errors in Detecting Upper Bounds . . . . . . . . . . . . 6-14 Incompatibilities with MATLAB in Variable-Size Support for Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15 Incompatibility with MATLAB for Scalar Expansion . . . . . . . . . . . . . . . . . 6-15 Incompatibility with MATLAB in Determining Size of Variable-Size N-D Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16 Incompatibility with MATLAB in Determining Size of Empty Arrays . . . . 6-16 Incompatibility with MATLAB in Determining Class of Empty Arrays . . . . 6-18 Incompatibility with MATLAB in Matrix-Matrix Indexing . . . . . . . . . . . . . 6-18 Incompatibility with MATLAB in Vector-Vector Indexing . . . . . . . . . . . . . 6-19 Incompatibility with MATLAB in Matrix Indexing Operations for Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19 Incompatibility with MATLAB in Concatenating Variable-Size Matrices . . 6-20 Differences When Curly-Brace Indexing of Variable-Size Cell Array Inside Concatenation Returns No Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20 Variable-Sizing Restrictions for Code Generation of Toolbox Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22 Common Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22 Toolbox Functions with Restrictions for Variable-Size Data . . . . . . . . . . . 6-23 Generate Code With Implicit Expansion Enabled . . . . . . . . . . . . . . . . . . . 6-27 Output Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27 Additional Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27 Performance Variation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-29 x Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.