Code Generation

Generate C/C++ code and MEX functions for Statistics and Machine Learning Toolbox™ functions

MATLAB® Coder™ generates readable and portable C and C++ code from Statistics and Machine Learning Toolbox functions that support code generation. For example, you can classify new observations on hardware devices that cannot run MATLAB by deploying a trained support vector machine (SVM) classification model to the device using code generation.

You can generate C/C++ code for these functions in several ways:

  • Use saveLearnerForCoder, loadLearnerForCoder, and codegen (MATLAB Coder) for an object function of a machine learning model.

  • Use a coder configurer created by learnerCoderConfigurer for predict and update object functions of a machine learning model. Configure code generation options by using the configurer and update model parameters in the generated code.

  • Use codegen for other functions that support code generation.

You can also generate fixed-point C/C++ code for the prediction of some machine learning models. This type of code generation requires Fixed-Point Designer™.

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.

To learn about code generation, see Introduction to Code Generation.

For a list of functions that support code generation, see Function List (C/C++ Code Generation).

Functions

expand all

saveLearnerForCoderSave model object in file for code generation
loadLearnerForCoderReconstruct model object from saved model for code generation
generateLearnerDataTypeFcnGenerate function that defines data types for fixed-point code generation

Create Coder Configurer Object

learnerCoderConfigurerCreate coder configurer of machine learning model

Work with Coder Configurer Object

generateCodeGenerate C/C++ code using coder configurer
generateFilesGenerate MATLAB files for code generation using coder configurer
validatedUpdateInputsValidate and extract machine learning model parameters to update
updateUpdate model parameters for code generation

Objects

expand all

ClassificationTreeCoderConfigurerCoder configurer of binary decision tree model for multiclass classification
ClassificationSVMCoderConfigurerCoder configurer for support vector machine (SVM) for one-class and binary classification
ClassificationLinearCoderConfigurerCoder configurer for linear binary classification of high-dimensional data
ClassificationECOCCoderConfigurerCoder configurer for multiclass model using binary learners
RegressionTreeCoderConfigurerCoder configurer of binary decision tree model for regression
RegressionSVMCoderConfigurerCoder configurer for support vector machine (SVM) regression model
RegressionLinearCoderConfigurerCoder configurer for linear regression model with high-dimensional data

Blocks

ClassificationSVM PredictClassify observations using support vector machine (SVM) classifier for one-class and binary classification
RegressionSVM PredictPredict responses using support vector machine (SVM) regression model

Topics

Code Generation Workflows

Introduction to Code Generation

Learn how to generate C/C++ code for Statistics and Machine Learning Toolbox functions.

General Code Generation Workflow

Generate code for Statistics and Machine Learning Toolbox functions that do not use machine learning model objects.

Code Generation for Prediction of Machine Learning Model at Command Line

Generate code for the prediction of a classification or regression model at the command line.

Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App

Generate code for the prediction of a classification or regression model by using the MATLAB Coder app.

Code Generation for Prediction and Update Using Coder Configurer

Generate code for the prediction of a model using a coder configurer, and update model parameters in the generated code.

Code Generation and Classification Learner App

Train a classification model using the Classification Learner app, and generate C/C++ code for prediction.

Code Generation for Nearest Neighbor Searcher

Generate code for finding nearest neighbors using a nearest neighbor searcher model.

Specify Variable-Size Arguments for Code Generation

Generate code that accepts input arguments whose size might change at run time.

Train SVM Classifier with Categorical Predictors and Generate C/C++ Code

Convert categorical predictors to numeric dummy variables before fitting an SVM classifier and generating code.

Fixed-Point Code Generation for Prediction of SVM

Generate fixed-point code for the prediction of an SVM classification or regression model.

Code Generation for Probability Distribution Objects

Generate code that fits a probability distribution object to sample data and evaluates the fitted distribution object.

Generate Code to Classify Numeric Data in Table

Generate code for classifying numeric data in a table using a binary decision tree.

Code Generation Applications

Predict Responses Using RegressionSVM Predict Block

This example shows how to train a support vector machine (SVM) regression model using the Regression Learner app, and then use the RegressionSVM Predict block for response prediction in Simulink®.

Predict Class Labels Using ClassificationSVM Predict Block

This example shows how to use the ClassificationSVM Predict block for label prediction.

Predict Class Labels Using MATLAB Function Block

Generate code from a Simulink model that classifies data using an SVM model.

System Objects for Classification and Code Generation

Generate code from a System object™ for making predictions using a trained classification model, and use the System object in a Simulink model.

Predict Class Labels Using Stateflow

Generate code from a Stateflow® model that classifies data using a discriminant analysis classifier.

Featured Examples