introduction to code generation -凯发k8网页登录

main content

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.

generating c/c code requires matlab coder and has the following limitations:

  • you cannot call any function at the top level when generating code by using codegen (matlab coder). instead, call the function within an entry-point function, and then generate code from the entry-point function. the entry-point function, also known as the top-level or primary function, is a function you define for code generation. all functions within the entry-point function must support code generation.

  • the matlab coder limitations also apply to statistics and machine learning toolbox for code generation. for details, see (matlab coder).

  • code generation in statistics and machine learning toolbox does not support sparse matrices.

  • for the code generation usage notes and limitations for each function, see the code generation section on the function reference page.

for a list of statistics and machine learning toolbox functions that support code generation, see .

code generation workflows

you can generate c/c code for the statistics and machine learning toolbox functions in several ways.

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

    general code generation workflow

    define an entry-point function that calls the function that supports code generation, generate c/c code for the entry-point function by using codegen (matlab coder), and then verify the generated code. the entry-point function, also known as the top-level or primary function, is a function you define for code generation. because you cannot call any function at the top level using codegen, you must define an entry-point function. all functions within the entry-point function must support code generation.

    for details, see .

  • code generation workflow for the object function of a machine learning model (including predict, random, knnsearch, rangesearch, isanomaly, and incremental learning object functions)

    code generation workflow for the object function of a machine learning model

    save a trained model by using savelearnerforcoder, and define an entry-point function that loads the saved model by using loadlearnerforcoder and calls the object function. then generate code for the entry-point function by using codegen (matlab coder), and verify the generated code. the input arguments of the entry-point function cannot be classification or regression model objects. therefore, you need to work around this limitation by using savelearnerforcoder and loadlearnerforcoder.

    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 'datatype','single' as an additional input to the loadlearnerforcoder function.

    for details, see these examples

    you can also generate fixed-point c/c code for the prediction of a support vector machine (svm) model, a decision tree model, and an ensemble of decision trees for classification and regression. this type of code generation requires fixed-point designer™.

    fixed-point code generation workflow

    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 by generatelearnerdatatypefcn, and use the structure as an input argument of loadlearnerforcoder in an entry-point function. you can also optimize the fixed-point data types before generating code.

    for details, see fixed-point code generation for prediction of svm.

  • code generation workflow for the predict and update functions 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

    code generation workflow for the predict and update functions with a coder configurer

    after training a model, create a coder configurer by using learnercoderconfigurer, generate code by using , and then verify the generated code. you can configure code generation options and specify the coder attributes of the model parameters using object properties. after you retrain the model with new data or settings, you can update model parameters in the generated c/c code without having to regenerate the code. this feature reduces the effort required to regenerate, redeploy, and reverify c/c code.

    for details, see code generation for prediction and update using coder configurer.

code generation applications

to integrate the prediction of a machine learning model into simulink®, use a matlab function block or the simulink blocks in the statistics and machine learning toolbox library. for details, see these examples:

code generation for the statistics and machine learning toolbox functions also works with other toolboxes such as system object™ and stateflow®, as described in these examples:

for more applications of code generation, see these examples:

see also

(matlab coder) | | | |

related topics

网站地图