Fit a generalized linear regression model, and then save the model by using `saveLearnerForCoder`

. Define an entry-point function that loads the model by using `loadLearnerForCoder`

and calls the `predict`

function of the fitted model. Then use `codegen`

to generate C/C++ code. Note that generating C/C++ code requires MATLAB® Coder™.

This example briefly explains the code generation workflow for the prediction of linear regression models at the command line. For more details, see Code Generation for Prediction of Machine Learning Model at Command Line. You can also generate code using the MATLAB Coder app. For details, see Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App.

**Train Model**

Generate sample data using Poisson random numbers with two underlying predictors `X(:,1)`

and `X(:,2)`

.

Create a generalized linear regression model. Specify the Poisson distribution for the response.

**Save Model**

Save the fitted generalized linear regression model to the file `GLMMdl.mat`

by using `saveLearnerForCoder`

.

**Define Entry-Point Function**

In your current folder, define an entry-point function named `mypredictGLM.m`

that does the following:

Accept new predictor input and valid name-value pair arguments.

Load the fitted generalized linear regression model in `GLMMdl.mat`

by using `loadLearnerForCoder`

.

Return predictions and confidence interval bounds.

function [yhat,ci] = mypredictGLM(x,varargin) %#codegen
%MYPREDICTGLM Predict responses using GLM model
% MYPREDICTGLM predicts responses for the n observations in the n-by-1
% vector x using the GLM model stored in the MAT-file GLMMdl.mat,
% and then returns the predictions in the n-by-1 vector yhat.
% MYPREDICTGLM also returns confidence interval bounds for the
% predictions in the n-by-2 vector ci.
CompactMdl = loadLearnerForCoder('GLMMdl');
narginchk(1,Inf);
[yhat,ci] = predict(CompactMdl,x,varargin{:});
end

Add the `%#codegen`

compiler directive (or pragma) to the entry-point function after the function signature to indicate that you intend to generate code for the MATLAB algorithm. Adding this directive instructs the MATLAB Code Analyzer to help you diagnose and fix violations that would result in errors during code generation.

**Generate Code**

Generate code for the entry-point function using `codegen`

. Because C and C++ are statically typed languages, you must determine the properties of all variables in the entry-point function at compile time. To specify the data type and exact input array size, pass a MATLAB® expression that represents the set of values with a certain data type and array size. Use `coder.Constant`

for the names of name-value pair arguments.

Create points for prediction.

Generate code and specify returning 90% simultaneous confidence intervals on the predictions.

`codegen`

generates the MEX function `mypredictGLM_mex`

with a platform-dependent extension.

If the number of observations is unknown at compile time, you can also specify the input as variable-size by using `coder.typeof`

. For details, see Specify Variable-Size Arguments for Code Generation and Specify Properties of Entry-Point Function Inputs (MATLAB Coder).

**Verify Generated Code**

Compare predictions and confidence intervals using `predict`

and `mypredictGLM_mex`

. Specify name-value pair arguments in the same order as in the `-args`

argument in the call to `codegen`

.

The returned values from `mypredictGLM_mex`

might include round-off differences compared to the values from `predict`

. In this case, compare the values allowing a small tolerance.

ans =
0x1 empty double column vector

ans =
0x1 empty double column vector

The comparison confirms that the returned values are equal within the tolerance `1e–6`

.