How to use prediction model from Classification Learner App in Simulink? ("Dot notation not allowed")

1 view (last 30 days)
I exported a prediction model "PredictionModel" from the Classification Learner App. I want to use this model in Simulink - either in a Matlab Function block or in a Matlab Function box in Stateflow.
In the command line window it works perfectly like this:
>> Maxtemp=6
Maxtemp =
6
>> Precipitation=3
Precipitation =
3
>> T=table(Maxtemp,Precipitation,'VariableNames',{'modeTmax','modePrecip'})
T =
1×2 table
modeTmax modePrecip
________ __________
6 3
>> y=PredictionModel.predictFcn(T)
y =
2
So this is one of the code varieties I tried for a Matlab Function box in Stateflow (without the % sign that seems needed for formatting in this forum):
%
function pred=Prediction(Tmax,Precip)
coder.extrinsic('table');
coder.extrinsic('PredictionModel');
coder.extrinsic('PredictionModel.predictFcn');
T=table(Tmax,Precip,'VariableNames',{'modeTmax','modePrecip'});
pred=PredictionModel.predictFcn(T);
end
but apparently I cannot call predictFcn this way, because I'm getting an error message "Dot notation on function call return value is not allowed." Using 'pred=predictFcn(T)'instead; doesn't help either (-> "Undefined function or variable 'predictFcn'").
The same problems when trying to use the prediction model in a Matlab Function block in Simulink.
Is there a workaround for this?
  1 Comment
Wilfred
Wilfred on 20 Nov 2017
I found this possible solution, based on which I simplified to
%
function pred=Prediction(Tmax,Precip)
coder.extrinsic('PredictionModel');
pred=zeros(1,2);
pred=PredictionModel(Tmax,Precip);
end %
Using this, I am getting no compilation errors any more, but the following message running the simulation:
"An error occurred while running the simulation and the simulation was terminated
Caused by: Undefined function 'PredictionModel' for input arguments of type 'xxxxx'."
with xxxxx whatever type I assign to Tmax and Precip (double, single, int8, etc.).
Suggestions how to make this work are still appreciated

Sign in to comment.

Accepted Answer

Arie Weeren
Arie Weeren on 28 Nov 2017
Dear Wilfred,
In order to use the prediction model that you created using the classification learner, you have to save the model that is stored in the PredictionModel. For instance, if it is a Discriminant model, you would use
saveCompactModel(PredictionModel.ClassificationDiscriminant,'ClassificationLearnerModel');
Then, you can use it in Simulink like
function pred = Prediction(Tmax,Precip)
persistent mdl;
if isempty(mdl)
mdl = loadCompactModel('ClassificationLearnerModel');
end
pred = predict(mdl,[Tmax Precip]);
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!