How to use the exported model from 'Regression Learner' APP inside the Matlab Simulink?

9 views (last 30 days)
Hello Everyone,
Recently, I used the Regression Learner App to train a model that predicts the output power of a solar panel based on the input solar irradiance and panel temperature. Afterwards, I exported the model as 'SolarModel' for future use. I can easily use this model by using the 'SolarModel.predictFcn([irradiance temperature])' in the 'command window'.
My main purpose is using the exported model inside the Simulink. I am wondering if anyone can help me that? To test that I used a very simple Simulink model using the 'MATLAB Function' block with two constant as input. See the following image.
The Matlab block function is:
function y = fcn(u)
y = SolarModel.predictFcn([u(1) u(2)]);
However, it gives me the following error upon running.
"Undefined function or variable 'SolarModel'.
Function 'MATLAB Function' (#296.25.35), line 3, column 5: "SolarModel" Launch diagnostic report."
I really appreciate if anyone can help me with that. I am very confused.

Answers (2)

diego alexis aragon sotelo
can you resolve your issue? i have the same problem with simulink.

Wei Huang
Wei Huang on 20 Jul 2019
Hello Mehrdad,
Not sure if you have this resolved but there is a work around to implement your regression learner model on simulink. It will require you to build an S-Function using codegen and legacy_code. You will need to install a C/C++ compiler. You can install MinGW-w64 C/C++ Compiler from MatLab add ons.
1) Once you have a trained model from the regression learner app and exported into your workspace, the model will be exported as a struct.
2) You want to use the command saveCompactModel(Mdl,'TrainedModel')
This will make your trained model, Mdl compact, and then saves it to the MATLAB binary file TrainedModel.mat as a structure array in the current folder.
Note: There are some regression models that are not compactible with the command saveCompactModel(). Mdl is the actual regression model in the struct of the exported variable with all the parameters of the regression model.
3) Write a function for your trained model, make sure you include %#codegen line
function y = predictor(X)
%#codegen
Mdl = loadCompactModel('TrainedModel');
label = predict(Mdl,X);
end
Note: X should have as many columns as the number of predictors that was used for training.
4) Type in command codegen -config:dll -args {X} into your workspace
Note: When I implemented my model into simulink, I wanted the S-Function to feed in a single output for a single time varying signal input, as oppose to an array of inputs and outputs. So my X in -args {X} was just a single value inside the within the range of my predictor variable.
5) Step 4 will generate a folder called codegen into your directory containing C code header and source files for your regression model. You will need to use the legacy code tool to build an S-Function block to be implemented into Simulink.
Write the following script and run it in the same directory that holds all your source and header files:
% Initialization Step
def = legacy_code('initialize');
%Link all .c and .h files that are in the current directory
%Obviously you will need to edit this to add your own files
def.SourceFiles = {'CompactModel.c' ..............};
def.HeaderFiles = {'doubleIt.h' ..................};
%Name your S-Function
def.SFunctionName = 'Trained_sfcn';
%Define S-Function inputs and outputs
%Look for source file 'predictor.c' in your directory, this file name depends on what you called %your function during step 3
%You will need to change def.OutputFcnSpec to the input and output specifications defined in your %'predictor.c' file.
def.OutputFcnSpec = 'double y = (double X)';
%Creating 'Trained_sfcn.c' file and tlc file
legacy_code('sfcn_cmex_generate', def);
legacy_code('sfcn_tlc_generate', def);
%Compile
legacy_code('compile', def);
6) After you've ran the above script successfully without any errors, the files 'Trained_sfcn.c' 'Trained_sfcn.tlc' 'Trained_sfcn.mexw64' should be in your directory.
7) Open simulink and use the S-Function block. In system name, put in Trained_sfcn (You do not need to add the .c part when putting in the name to S-Function block). To run this successfully, you need to addpath('codegen') and the subfolder that contained Trained_sfcn.c when you are in the directory you're running your simlink model from.
8) This has proven to work for my model on a Windows 10 computer. I hope this helped.
  2 Comments
Mona Faraji Niri
Mona Faraji Niri on 3 Jun 2021
Edited: Mona Faraji Niri on 3 Jun 2021
Hi Wei, I am trying to follow this answer of yours, but I get errors for legacy_code('sfcn_cmex_generate', def);
error:
Cannot find a valid lhs expression in the function specification (only scalar output can be
specified as the return of the function).
would you please let me know if you recognsise the error? if the input to the model is list of 10 features, so X is an array, but I still type double should be OK for it?
should I have X loaded in the Workspace to have codegen fcn -args {X} work?
cheers

Sign in to comment.

Categories

Find more on Event Functions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!