主要内容

Introduction to Code Generation

MATLAB®Coder™generates readable and portable C and C++ code from Statistics and Machine Learning Toolbox™ functions that support code generation. You can integrate the generated code into your projects as source code, static libraries, or dynamic libraries. You can also use the generated code within the MATLAB environment to accelerate computationally intensive portions of your MATLAB code.

生成C/C ++代码需要MATLAB Coder并具有以下限制:

  • 通过使用时,您不能在生成代码时在最高级别调用任何功能codegen(MATLAB编码器)。而是在一个内调用该功能入口点function, and then generate code from the entry-point function. The entry-point function, also known as thetop-level或者基本的function, is a function you define for code generation. All functions within the entry-point function must support code generation.

  • TheMATLAB Coderlimitations also apply to Statistics and Machine Learning Toolbox for code generation. For details, seeMATLAB语言功能支持C/C ++代码生成金宝app(MATLAB编码器)

  • 统计和机器学习工具箱中的代码生成不支持稀疏矩阵和分类数组。金宝app代码生成支持大多数预测功能的数字金宝app表。

  • 有关每个功能的代码生成用法注释和限制,请参见“函数参考”页面上的代码生成部分。

For a list of Statistics and Machine Learning Toolbox functions that support code generation, seeFunction List (C/C++ Code Generation)

代码生成工作流程

您可以通过多种方式生成统计信息和机器学习工具箱功能的C/C ++代码。

  • General code generation workflow for functions that are not the object functions of machine learning models

    定义入口点function that calls the function that supports code generation, generate C/C++ code for the entry-point function by usingcodegen(MATLAB编码器),然后验证生成的代码。The entry-point function, also known as thetop-level或者基本的function, is a function you define for code generation. Because you cannot call any function at the top level usingcodegen, you must define an entry-point function. All functions within the entry-point function must support code generation.

    For details, seeGeneral Code Generation Workflow

  • 对象函数的代码生成工作流(预测,random,knnsearch, orrangesearch) of a machine learning model

    Save a trained model by usingSavelearnerforcoder并定义一个入口点功能,该功能通过使用loadLearnerForCoder和calls the object function. Then generate code for the entry-point function by usingcodegen(MATLAB编码器),并验证生成的代码。入口点函数的输入参数不能是分类或回归模型对象。因此,您需要使用SavelearnerforcoderloadLearnerForCoder

    You can also generate single-precision C/C++ code for the prediction of machine learning models for classification and regression. For single-precision code generation, specify the name-value pair argument“数据类型”,“单个”as an additional input to theloadLearnerForCoderfunction.

    有关详细信息,请参见这些示例

    您还可以生成定点C/C ++代码,以预测支持向量机(SVM)模型,决策树模型以及用于分类和回归的决策树的集合。金宝app这种代码生成需要定点Designer™。

    Fixed-point code generation requires an additional step that defines the fixed-point data types of the variables required for prediction. Create a fixed-point data type structure by using the data type function generated bygeneratelearnerdatatypefcn, and use the structure as an input argument ofloadLearnerForCoderin an entry-point function. You can also optimize the fixed-point data types before generating code.

    For details, see固定点代码生成用于SVM的预测

  • Code generation workflow for the预测updatefunctions of a tree model, an SVM model, a linear model, or a multiclass error-correcting output codes (ECOC) classification model using SVM or linear binary learners

    通过使用LEARNERNERNECODERCONFIGURER,通过使用GenerateCode,然后验证生成的代码。您可以配置代码生成选项并使用对象属性指定模型参数的编码器属性。在使用新数据或设置进行重新验证模型后,您可以在生成的C/C ++代码中更新模型参数,而无需再生代码。此功能减少了再生,重新部署和可忽视的C/C ++代码所需的努力。

    For details, see用于预测和使用编码器配置器更新的代码生成

Code Generation Applications

To integrate the prediction of a machine learning model into Simulink®,在统计信息和机器学习工具箱库中使用MATLAB功能块或Simulink块金宝app。有关详细信息,请参见以下示例:

统计和机器学习工具箱功能的代码生成还可以与其他工具箱(例如System Object™和stateFlow)一起使用®, as described in these examples:

有关代码生成的更多应用,请参见以下示例:

See Also

||||(MATLAB编码器)

相关话题