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

조회 수: 1 (최근 30일)
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
Wilfred
Wilfred 2017년 11월 20일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Arie Weeren
Arie Weeren 2017년 11월 28일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Classification Learner App에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by