Main Content

Generate Code for Predicting Remaining Useful Life

This example shows how to use MATLAB® Coder to deploy an algorithm for predicting remaining useful life (RUL). Such code generation is useful when you have trained an RUL prediction model such as a linearDegradationModel in MATLAB and are ready to deploy the prediction algorithm to another environment. This example uses MATLAB Coder to generate a MEX file that is executable from MATLAB. You can use a similar procedure to generate code for any target that MATLAB Coder supports.

The workflow for generating code for predicting RUL is illustrated in the following diagram. The first step is to fit an RUL model using historical data from your system, as described in Update RUL Prediction as Data Arrives. You must also write an entry-point function from which to generate code. This function is a MATLAB function that receives new data from your system and uses it to predict a new RUL.

You can use the workflow of this example to generate code for predicting remaining useful life with degradation-based RUL models, linearDegradationModel and exponentialDegradationModel.

Fit RUL Model

Before generating code for RUL prediction, you must fit an RUL model using historical data. For this example, load the data in linTrainTables.mat. This file contains measurements of some condition indicator taken over time, organized into tables with column labels "Time" and "Condition". Use this data to train a linear degradation model. (For more information about configuring and training this type of RUL model, see linearDegradationModel.)


mdl = linearDegradationModel;

Prepare Model for Code Generation

Once you have a trained RUL model, save the model using saveRULModelForCoder. This function saves the RUL model to a MAT file. Later, in the entry-point function, use loadRULModelForCoder to load and reconstruct the RUL model from that file.

saveMATfilename = 'savedModel.mat';

Define Entry-Point Function

The entry-point function is the function for which you want to generate code. When predicting RUL, your entry-point function might take input data, process it in some way to extract the condition indicator, and then use predictRUL to obtain a new RUL estimate from the model.

For this example, create the entry-point function degradationRULPredict.m, as shown here.

type degradationRULPredict.m
function [estRUL,ci,pdfRUL] = degradationRULPredict(data)

threshold = 60;

% Load prepared model
mdl = loadRULModelForCoder('savedModel.mat');

% Use input data for new prediction
[estRUL,ci,pdfRUL] = predictRUL(mdl,data,threshold);


This function takes as input a data point consisting of a time and a condition-indicator value. The function uses loadRULModelForCoder to load the version of the trained model previously saved with saveRULModelForCoder. The function also includes the required %#codegen directive, which instructs the Check Code with the Code Analyzer (MATLAB Coder) to help you diagnose and fix violations that might result in errors during code generation or at runtime.

Capabilities and Limitations of the Entry-Point Function

The simple entry-point function of this example loads the model and obtains the new RUL prediction. Your entry-point function can do other operations, such as further processing on input data to extract a condition indicator for use in the prediction. However, all functions and operations within the entry-point function must support code generation.

Your function can also use the update command to update the prediction model based on the new data. To do so, you can include additional code to preserve the updated model parameters when you shut down and restart the deployed system. For more information, see Generate Code that Preserves RUL Model State for System Restart.

Your entry-point function can generally change properties of the RUL model object that are not read-only. However, the values of the following properties, which are otherwise read-write, are fixed in generated code. Attempting to change them at run time generates an error.

  • LifeTimeVariable

  • LifeTimeUnit

  • DataVariables

Generate Code

To generate code, you must provide sample data having the data type and format expected by the entry-point function. For this example, load some test data in the same format as the data you used to train the RUL model, tables of times and condition-indicator values. Because your entry-point function takes one time and value as its input, extract one entry from the table of test data. For code generation, the specific values do not matter, only the data types.

testData = linTestData1(1,:);
testData=1×2 table
    Time    Condition
    ____    _________

     1       2.1316  

Open MATLAB Coder App and Specify Entry-Point Function

In the MATLAB desktop, on the Apps tab, under Code Generation, click MATLAB Coder. The MATLAB Coder app opens on the Select Source Files page. In the Generate code for function box, enter the name of the entry-point function, degradationRULPredict. Then, click Next.

Specify Input Data Types

To specify the input data types for the entry-point function, on the Define Input Types page, use testData in a call to degradationRULPredict. When you enter the call, MATLAB Coder displays the detected input types and number of outputs. Click Next to confirm.

Generate Code

Optionally, check the entry-point function for issues arising at run time. To do so, click Check for Issues. When you are ready, click Next to advance to the Generate Code page. In this page, you specify the target for code generation. You can generate RUL prediction code for any of the targets that MATLAB Coder supports, including standalone C/C++ code, C/C++ code compiled to a library, or C/C++ code compiled to an executable. For this example, from the Build type list, select MEX. A MEX file is an executable that you can call from within MATLAB.

Click Generate to generate the MEX file, degradationRULPredict_mex. For additional information about MATLAB Coder capabilities and the files it generates, see Generate C Code by Using the MATLAB Coder App (MATLAB Coder).

Generate Code with codegen Command

As an alternative to using the MATLAB Coder app, you can generate code using the following codegen (MATLAB Coder) command.

codegen degradationRULPredict -args {testData} -nargout 3
Code generation successful.

Validate Generated Code

To validate the generated code, at the MATLAB command prompt, run the entry-point MATLAB function on the test data. Then, run the generated MEX file on the same data and confirm that the results are the same.

[estRUL,ci,pdfRUL] = degradationRULPredict(testData);

[estRUL_mex,ci_mex,pdfRUL_mex] = degradationRULPredict_mex(testData);

For example, compare the estimated RUL obtained with the MATLAB function and the generated MEX file.

estRUL = 114.2927
estRUL_mex = 114.2927

You can now use the generated code as part of your deployed system for predicting remaining useful life.

See Also

| |

Related Topics