Generating C/C++ code requires MATLAB® Coder™. 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.
For a list of the code-generation-enabled functions in Statistics and Machine Learning Toolbox, see Code Generation Support, Usage Notes, and Limitations. For the code generation usage notes and limitations for each function, see the Code Generation section on the function reference page. Note that the MATLAB Coder limitations apply to Statistics and Machine Learning Toolbox for code generation. For details, see MATLAB Language Features Supported for C/C++ Code Generation (MATLAB Coder). In addition to these limitations, code generation in Statistics and Machine Learning Toolbox does not support sparse matrices.
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
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, 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 General Code Generation Workflow.
Code generation workflow for the object function (
rangesearch) of a machine learning model
Save a trained model by using
saveCompactModel, and define an entry-point function that loads the
saved model by using
loadCompactModel and calls the object
function. Then generate code for the entry-point function by using
codegen, 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
For details, see these examples
Code generation workflow for the
functions of a support vector machine (SVM) model or a muticlass error-correcting
output codes (ECOC) classification model using SVM binary learners:
Create a coder configurer by using
learnerCoderConfigurer, generate code by using
generateCode, 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
For details, see Code Generation for Prediction and Update Using Coder Configurer.
Code generation for the Statistics and Machine Learning Toolbox functions also works with other toolboxes such as Simulink®, System object™, and Stateflow®, as described in these examples:
For more applications of code generation, see these examples: