Code Generation From MATLAB User’s Guide

Transcription

Code Generation from MATLAB User’s GuideR2011b

How to Contact MathWorksWebNewsgroupwww.mathworks.com/contact TS.html Technical service@mathworks.cominfo@mathworks.comProduct enhancement suggestionsBug reportsDocumentation error reportsOrder status, license renewals, passcodesSales, pricing, and general information508-647-7000 (Phone)508-647-7001 (Fax)The MathWorks, Inc.3 Apple Hill DriveNatick, MA 01760-2098For contact information about worldwide offices, see the MathWorks Web site.Code Generation from MATLAB User’s Guide COPYRIGHT 2007–2011 by The MathWorks, Inc.The software described in this document is furnished under a license agreement. The software may be usedor copied only under the terms of the license agreement. No part of this manual may be photocopied orreproduced in any form without prior written consent from The MathWorks, Inc.FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentationby, for, or through the federal government of the United States. By accepting delivery of the Programor Documentation, the government hereby agrees that this software or documentation qualifies ascommercial computer software or commercial computer software documentation as such terms are usedor defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms andconditions of this Agreement and only those rights specified in this Agreement, shall pertain to and governthe use, modification, reproduction, release, performance, display, and disclosure of the Program andDocumentation 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 thegovernment’s needs or is inconsistent in any respect with federal procurement law, the government agreesto return the Program and Documentation, unused, to The MathWorks, Inc.TrademarksMATLAB and Simulink are registered trademarks of The MathWorks, Inc. Seewww.mathworks.com/trademarks for a list of additional trademarks. Other product or brandnames may be trademarks or registered trademarks of their respective holders.PatentsMathWorks products are protected by one or more U.S. patents. Please seewww.mathworks.com/patents for more information.

Revision HistoryMarch 2007September 2007March 2008October 2008March 2009September 2009March 2010September 2010April 2011September onlyNew for Release 2007aRevised for Release 2007bRevised for Release 2008aRevised for Release 2008bRevised for Release 2009aRevised for Release 2009bRevised for Release 2010aRevised for Release 2010bRevised for Release 2011aRevised for Release 2011b

ContentsAbout Code Generation from MATLABAlgorithms1Direct Translation of MATLAB Algorithms to C/C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-2Prerequisites for Code Generation from MATLAB . . . .1-3Preparing MATLAB Code for C/C and MEX CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-4Expected Differences in Behavior After Compiling YourMATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Why Are There Differences? . . . . . . . . . . . . . . . . . . . . . . . . .Character Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Order of Evaluation in Expressions . . . . . . . . . . . . . . . . . . .Termination Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Size of Variable-Size N-D Arrays . . . . . . . . . . . . . . . . . . . . .Floating-Point Numerical Results . . . . . . . . . . . . . . . . . . . .NaN and Infinity Patterns . . . . . . . . . . . . . . . . . . . . . . . . . .Code Generation Target . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-51-51-51-51-61-61-71-71-7MATLAB Language Features Supported for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-9MATLAB Language Features Not Supported for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-11Related Products That Support Code Generation fromMATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-12v

Functions Supported for Code Generation2viContentsAbout Code Generation for Supported Functions . . . . .2-2Functions Supported for Code Generation —Alphabetical List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-3Functions Supported for Code Generation —Categorical List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Aerospace Toolbox Functions . . . . . . . . . . . . . . . . . . . . . . . .Arithmetic Operator Functions . . . . . . . . . . . . . . . . . . . . . .Bit-Wise Operation Functions . . . . . . . . . . . . . . . . . . . . . . .Casting Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Communications System Toolbox Functions . . . . . . . . . . . .Complex Number Functions . . . . . . . . . . . . . . . . . . . . . . . . .Computer Vision System Toolbox Functions . . . . . . . . . . . .Data Type Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Derivative and Integral Functions . . . . . . . . . . . . . . . . . . . .Discrete Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .Error Handling Functions . . . . . . . . . . . . . . . . . . . . . . . . . .Exponential Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Filtering and Convolution Functions . . . . . . . . . . . . . . . . . .Fixed-Point Toolbox Functions . . . . . . . . . . . . . . . . . . . . . . .Histogram Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Image Processing Toolbox Functions . . . . . . . . . . . . . . . . . .Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . .Interpolation and Computational Geometry . . . . . . . . . . . .Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Logical Operator Functions . . . . . . . . . . . . . . . . . . . . . . . . .MATLAB Compiler Functions . . . . . . . . . . . . . . . . . . . . . . .Matrix and Array Functions . . . . . . . . . . . . . . . . . . . . . . . . .Nonlinear Numerical Methods . . . . . . . . . . . . . . . . . . . . . . .Polynomial Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Relational Operator Functions . . . . . . . . . . . . . . . . . . . . . . .Rounding and Remainder Functions . . . . . . . . . . . . . . . . . .Set Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Signal Processing Functions in MATLAB . . . . . . . . . . . . . .Signal Processing Toolbox Functions . . . . . . . . . . . . . . . . . .Special Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Specialized Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22-822-832-832-842-842-892-892-90

String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Structure Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-902-912-91System Objects Supported for Code Generation3About Code Generation for System Objects . . . . . . . . . .3-2Computer Vision System Toolbox System Objects.3-3Communications System Toolbox System Objects.3-7DSP System Toolbox System Objects . . . . . . . . . . . . . . . .3-13Defining MATLAB Variables for C/C CodeGeneration4Why Define Variables Differently for CodeGeneration? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-2Best Practices for Defining Variables for C/C CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .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 . . . . .4-34-34-64-64-6When You Can Reassign Variable Properties for C/C Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-7Eliminating Redundant Copies of Variables inGenerated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-8vii

When Redundant Copies Occur . . . . . . . . . . . . . . . . . . . . . .How to Eliminate Redundant Copies by DefiningUninitialized Variables . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Uninitialized Variables . . . . . . . . . . . . . . . . . . . . .4-8Defining and Initializing Persistent Variables . . . . . . . .4-10Reusing the Same Variable with DifferentProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .When You Can Reuse the Same Variable with DifferentProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .When You Cannot Reuse Variables . . . . . . . . . . . . . . . . . . .Limitations of Variable Reuse . . . . . . . . . . . . . . . . . . . . . . .Supported Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . .4-84-94-114-114-124-144-16Defining Data for Code Generation5How Working with Data is Different for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-2Code Generation for Complex Data . . . . . . . . . . . . . . . . .Restrictions When Defining Complex Variables . . . . . . . . .Expressions Containing Complex Operands Yield ComplexResults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-45-4Code Generation for Characters . . . . . . . . . . . . . . . . . . . .5-65-5Code Generation for MATLAB Structures6How Working with Structures Is Different for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .viiiContents6-2

Structure Operations Allowed for Code Generation . . .6-3Defining Scalar Structures for Code Generation . . . . . .Restrictions When Using struct . . . . . . . . . . . . . . . . . . . . .Restrictions When Defining Scalar Structures byAssignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Adding Fields in Consistent Order on Each Control FlowPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Restriction on Adding New Fields After First Use . . . . . . .6-46-4Defining Arrays of Structures for Code Generation . . .Ensuring Consistency of Fields . . . . . . . . . . . . . . . . . . . . . .Using repmat to Define an Array of Structures withConsistent Field Properties . . . . . . . . . . . . . . . . . . . . . . .Defining an Array of Structures Using Concatenation . . . .6-76-7Making Structures Persistent . . . . . . . . . . . . . . . . . . . . . . .6-9Indexing Substructures and Fields . . . . . . . . . . . . . . . . . .6-10Assigning Values to Structures and Fields . . . . . . . . . . .6-12Passing Large Structures as Input Parameters . . . . . . .6-136-46-46-56-76-8Code Generation for Enumerated Data7How Working with Enumerated Data Is Different forCode Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-27-2Enumerated Types Supported for Code Generation . . .Enumerated Type Based on int32 . . . . . . . . . . . . . . . . . . . .Enumerated Type Based on Simulink.IntEnumType . . . . .7-37-37-4When to Use Enumerated Data for Code Generation . .7-6ix

Workflows for Using Enumerated Data for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Workflow for Generating Code for Enumerated Data fromMATLAB Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . .Workflow for Generating Code for Enumerated Data fromMATLAB Function Blocks . . . . . . . . . . . . . . . . . . . . . . . .7-87-97-10How to Instantiate Enumerated Types for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-11How to Generate Code for Enumerated Data . . . . . . . . .See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-127-12Defining and Using Enumerated Types for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .About the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Class Definition: sysMode . . . . . . . . . . . . . . . . . . . . . . . . . .Class Definition: LEDcolor . . . . . . . . . . . . . . . . . . . . . . . . . .Function: displayState . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-137-137-137-147-14Operations on Enumerated Data Allowed for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Assignment Operator, . . . . . . . . . . . . . . . . . . . . . . . . . . . .Relational Operators, . . . . . . . . . . . . . . . .Cast Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Indexing Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Control Flow Statements: if, switch, while . . . . . . . . . . . . .7-157-157-157-167-167-17Restrictions on Use of Enumerated Data infor-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Contents7-7How to Define Enumerated Data for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Naming Enumerated Types for Code Generation . . . . . . . .Using Enumerated Data in Control Flow Statements . .Using the if Statement on Enumerated Data Types . . . . .Using the switch Statement on Enumerated DataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the while Statement on Enumerated Data Types . .x7-77-187-187-197-227-24

Toolbox Functions That Support Enumerated Types forCode Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-25Code Generation for Variable-Size Data8What Is Variable-Size Data? . . . . . . . . . . . . . . . . . . . . . . . .8-2How Working with Variable-Size Data Is Different forCode Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-38-3Bounded Versus Unbounded Variable-Size Data . . . . . .8-4When to Use Dynamic Allocation for Variable-SizeData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-58-5How to Generate Code for MATLAB Functions withVariable-Size Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6Tutorial: Generating MEX Code for a MATLABFunction That Expands a Vector in a Loop . . . . . . . .About the MATLAB Function emldemo uniquetol . . . . . . .Step 1: Add Compilation Directive for Code Generation . .Step 2: Address Issues Detected by the Code Analyzer . . .Step 3: Generate MEX Code . . . . . . . . . . . . . . . . . . . . . . . . .Step 4: Fix the Size Mismatch Error . . . . . . . . . . . . . . . . . .Step 5: Fix the Upper Bounds Error . . . . . . . . . . . . . . . . . .Step 6: Generate C/C Code . . . . . . . . . . . . . . . . . . . . . . . .8-98-98-108-108-108-128-148-16Variable-Size Data in Code Generation Reports . . . . . .What Reports Tell You About Size . . . . . . . . . . . . . . . . . . . .How Size Appears in Code Generation Reports . . . . . . . . .How to Generate a Code Generation Report . . . . . . . . . . . .8-188-188-198-19Defining Variable-Size Data for Code Generation . . . . .8-20xi

When to Define Variable-Size Data Explicitly . . . . . . . . . .Using a Matrix Constructor with NonconstantDimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inferring Variable Size from Multiple Assignments . . . . . .Defining Variable-Size Data Explicitly Usingcoder.varsize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-20Specifying Upper Bounds for Variable-Size Data . . . . .When to Specify Upper Bounds for Variable-Size Data . . .Specifying Upper Bounds on the Command Line forVariable-Size Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Specifying Unknown Upper Bounds for Variable-SizeInputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Specifying Upper Bounds for Local Variable-Size Data . . .8-278-27C Code Interface for Unbounded Arrays and StructureFields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .emxArray: Representation of Data with Unknown UpperBounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Utility Functions for Creating emxArray DataStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Troubleshooting Issues with Variable-Size Data . . . . . .Diagnosing and Fixing Size Mismatch Errors . . . . . . . . . . .Diagnosing and Fixing Errors in Detecting UpperBounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Limitations with Variable-Size Support for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Limitation on Scalar Expansion . . . . . . . . . . . . . . . . . . . . . .Incompatibility with MATLAB in Determining Size ofVariable-Size N-D Arrays . . . . . . . . . . . . . . . . . . . . . . . . .Limitation on Vector-Vector Indexing . . . . . . . . . . . . . . . . .Limitations on Matrix Indexing Operations for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dynamic Memory Allocation Not Supported for MATLABFunction Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Limitation on Mixing Stack and Heap Allocation . . . . . . . .Restrictions on Variable Sizing in Toolbox FunctionsSupported for Code Generation . . . . . . . . . . . . . . . . . . .Common Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338-338-358-378-378-388-398-408-418-418-428-42

Toolbox Functions with Variable Sizing Restrictions . . . . .8-43Code Generation for Function Handles9How Working with Function Handles is Different forCode Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-2Example: Defining and Passing Function Handles forCode Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-3Limitations with Function Handles for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9-6Defining Functions for Code Generation10Specifying Variable Numbers of Arguments . . . . . . . . . .10-2Supported Index Expressions . . . . . . . . . . . . . . . . . . . . . . .10-3Using varargin and varargout in for-Loops . . . . . . . . . .When to Force Loop Unrolling . . . . . . . . . . . . . . . . . . . . . . .Example: Using Variable Numbers of Arguments in afor-Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10-410-4Implementing Wrapper Functions with varargin andvarargout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example: Passing Variable Numbers of Arguments fromOne Function to Another . . . . . . . . . . . . . . . . . . . . . . . . .Passing Property/Value Pairs with varargin . . . . . . . . .10-510-710-710-8xiii

Rules for Using Variable Length Argument Lists forCode Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10Calling Functions for Code Generation11How MATLAB Resolves Function Calls in GeneratedCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Key Points About Resolving Function Calls . . . . . . . . . . . .Compile Path Search Order . . . . . . . . . . . . . . . . . . . . . . . . .When to Use the Code Generation Path . . . . . . . . . . . . . . .11-211-411-411-5How MATLAB Resolves File Types on the Path for CodeGeneration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-6Adding the Compilation Directive %#codegen . . . . . . . .11-8Calling Subfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-9Calling Supported Toolbox Functions . . . . . . . . . . . . . . . 11-10Calling MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . .Declaring MATLAB Functions as Extrinsic Functions . . .Calling MATLAB Functions Using feval . . . . . . . . . . . . . . .How MATLAB Resolves Extrinsic Functions DuringSimulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with mxArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . .Restrictions on Extrinsic Functions for Code Generation . .Limit on Function Arguments . . . . . . . . . . . . . . . . . . . . . . .11-1111-1111-1511-1511-1611-1711-18Generating Efficient and Reusable Code12Generating Efficient Code . . . . . . . . . . . . . . . . . . . . . . . . . .xivContents12-2

Unrolling for-Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inlining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Eliminating Redundant Copies of Function Inputs . . . . . .12-212-212-2Generating Reusable Code . . . . . . . . . . . . . . . . . . . . . . . . .12-4ExamplesAData Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A-2Code Generation for Structures . . . . . . . . . . . . . . . . . . . . .A-2Code Generation for Enumerated Data . . . . . . . . . . . . . .A-2Code Generation for Variable-Size Data . . . . . . . . . . . . .A-2Code Generation for Function Handles . . . . . . . . . . . . . .A-3Using Variable-Length Argument Lists . . . . . . . . . . . . . .A-3Optimizing Generated Code . . . . . . . . . . . . . . . . . . . . . . . .A-3Indexxv

xviContents

1About Code Generationfrom MATLAB Algorithms “Direct Translation of MATLAB Algorithms to C/C Code” on page 1-2 “Prerequisites for Code Generation from MATLAB” on page 1-3 “Preparing MATLAB Code for C/C and MEX Code Generation” on page1-4 “Expected Differences in Behavior After Compiling Your MATLAB Code”on page 1-5 “MATLAB Language Features Supported for Code Generation” on page 1-9 “MATLAB Language Features Not Supported for Code Generation” onpage 1-11 “Related Products That Support Code Generation from MATLAB” on page1-12

1About Code Generation from MATLAB AlgorithmsDirect Translation of MATLAB Algorithms to C/C CodeYou can automatically generate MEX functions and standalone C/C codefrom MATLAB algorithms. With this capability, you can design, implement,and test software in the MATLAB environment, then automatically translatethe algorithms to readable, efficient, and compact C/C code for deploymentto desktop and embedded systems.The generated code contains optimizations tailored to meet the requirementsof desktop and embedded applications for speed, memory use, and data typemanagement.To verify the generated code in the MATLAB environment, you can generateMEX functions to compare with your original algorithm to determine whetherthey are functionally equivalent.In certain applications, you can also generate MEX functions to accelerateMATLAB algorithms.1-2

Prerequisites for Code Generation from MATLAB Prerequisites for Code Generation from MATLABTo generate C/C or MEX code from MATLAB algorithms, you must installthe following software: MATLAB Coder product C/C compilerFor more information, see: “Why Test MEX Functions in MATLAB?” in the MATLAB Coderdocumentation. “Generating C/C Code from MATLAB Code” in the MATLAB Coderdocumentation. “Accelerating MATLAB Algorithms” in the MATLAB Coder documentation.1-3

1About Code Generation from MATLAB AlgorithmsPreparing MATLAB Code for C/C and MEX CodeGenerationBy default, the MATLAB language uses dynamic typing. Properties ofdynamically typed variables can change at runtime, allowing a single variableto hold a value of any class, size, or complexity. However, to generate efficientcode for statically typed languages such as C and C , the properties of allMATLAB variables must be determined at compile time. Therefore, to prepareMATLAB code for C, C , and MEX code generation, you must specify theclass, size, and complexity of inputs to the primary function (also known as thetop-level or entry-point function). By determining these properties at compiletime, the code generation process translates your MATLAB algorithms intocode that is efficient and tailored to your specific application, rather thanproducing generic code that handles every possible set of MATLAB inputs.1-4

Expected Differences in Behavior After Compiling Your MATLAB CodeExpected Differences in Behavior After Compiling YourMATLAB CodeIn this section.“Why Are There Differences?” on page 1-5“Character Size” on page 1-5“Order of Evaluation in Expressions” on page 1-5“Termination Behavior” on page 1-6“Size of Variable-Size N-D Arrays” on page 1-6“Floating-Point Numerical Results” on page 1-7“NaN and Infinity Patterns” on page 1-7“Code Generation Target” on page 1-7Why Are There Differences?To convert MATLAB code to C/C code that works efficiently, the codegeneration process introduces optimizations that intentionally cause thegenerated code to behave differently — and sometimes produce differentresults — from the original source code. This section describes thesedifferences.Character SizeMATLAB supports 16-bit characters, but the generated code representscharacters in 8 bits, the standard size for most embedded languages like C.See “Code Generation for Characters” on page 5-6.Order of Evaluation in ExpressionsGenerated code does not enforce order of evaluation in expressions. For mostexpressions, order of evaluation is not significant. However, for expressionswith side effects, the generated code may produce the side effects in differentorder from the original MATLAB code. Expressions that produce side effectsinclude those that:1-5

1About Code Generation from MATLAB Algorithms Modify persistent or global variables Display data to the screen Write data to filesIn addition, the generated code does not enforce order of evaluation of logicaloperators that do not short circuit.For more predictable results, it is good coding practice to split expressionsthat depend on the order of evaluation into multiple statements. For example,rewrite:A f1() f2();asA f1();A A f2();so that the generated code calls f1 before f2.Termination BehaviorGenerated code does not match the termination behavior of MATLAB sourcecode. For example, optimizations remove infinite loops from generated code ifthey have no side effects. As a result, the generated code may terminate eventhough the corresponding MATLAB code does not.Size of Variable-Size N-D ArraysFor variable-size N-D arrays, the size function might return a differentresult in generated code than in MATLAB source code. The size functionsometimes returns trailing ones (singleton dimensions) in generated code, butalways drops trailing ones in MATLAB. For example, for an N-D array X withdimensions [4 2 1 1], size(X) might return [4 2 1 1] in generated code,but always returns [4 2] in MATLAB. See “Incompatibility with MATLAB inDetermining Size of Variable-Size N-D Arrays” on page 8-38.1-6

Expected Differences in Behavior After Compiling Your MATLAB CodeFloating-Point Numerical ResultsThe generated code might not produce the same floating-point numericalresults as MATLAB in the following situations:When computer hardware uses extended precision registersResults vary depending on how the C/C compiler allocates extendedprecision floating-point registers. Computation results might not matchMATLAB calculations because of different compiler optimization settings ordifferent code surrounding the floating-point calculations.For certain advanced library functionsThe generated code might use different algorithms to implement certainadvanced library functions, such as fft, svd, eig, mldivide, and mrdivide.For example, the generated code uses a simpler algorithm to implementsvd to accommodate a smaller footprint. Results might also vary accordingto matrix properties. For example, MATLAB might detect symmetric orHermitian matrices at run time and switch to specialized algorithms thatperform computations faster than implementations in the generated code.For implementation of BLAS library functionsFor implementations of BLAS library functions. Generated C/C code usesreference implementations of BLAS functions, which may produce differentresults from platform-specific BLAS implementations in MATLAB.NaN and Infinity PatternsThe generated code might not produce exactly the same pattern of NaN and infvalues as MATLAB code when these values are mathematically meaningless.For example, if MATLAB output contains a NaN, output from the generatedcode should also contain a NaN, but not necessarily in the same place.Code Generation TargetThe coder.target function returns different values in MATLAB than in thegenerated code. The intent is to help you determine whether your function1-7

1About Code Generation from MATLAB Algorithmsis executing in MATLAB or has been compiled for a simulation or codegeneration target. See coder.target.1-8

MATLAB Language Features Supported for Code GenerationMATLAB Language Features Supported for CodeGenerationMATLAB supports the following language features

About Code Generation from MATLAB Algorithms 1 Direct Translation of MATLAB Algorithms to C/C Code.1-2 Prerequisites for Code Generation from MATLAB. 1-3 Preparing MATLAB Code for C/C and MEX Code Generation.1-4 Expected Differences in Behavior After Compiling YourFile Size: 991KBPage Count: 301