Error using Classificationlearner model in Simulink

조회 수: 9 (최근 30일)
Yougzz
Yougzz 2019년 5월 9일
댓글: Le Truong An 2019년 7월 12일
I used Classification learner App with a Fine KNN model that I called 'model_test'.
Now I want to use it in simulink.
So, I used a function block with the inputs of my model and 'model_test' as a parameter :
function y = predic(Fflown,N1n1,N2n1,N3n1,Taun1, model_test)
coder.extrinsic('model_test');
coder.extrinsic('model_test.predictFcn');
T=[Fflown,N1n1,N2n1,N3n1,Taun1];
y=0;
y = model_test.predictFcn(T);
end
But I run the simulink model, I have the error :
'MATLAB class 'function_handle' found at 'model_test.predictFcn' is unsupported.
Parameter 'model_test''
I saw that I may should use save CompactModel to export my Classification learner but when I try it I have another error :
"Undefined function 'toStruct' for input arguments of type 'ClassificationKNN'.
Error in saveCompactModel (line 17)
classificationStruct = toStruct(classificationObj); %#ok<NASGU>""

채택된 답변

Sean de Wolski
Sean de Wolski 2019년 5월 9일
편집: Sean de Wolski 2019년 5월 9일
Unfortunately this workflow is more painful than it should be. I've found the best set of steps is to:
  • Generate the code for the model in classification learner.
  • Copy out the relevant parts that call the classification training algorithm into a script, in this case fitcknn, and then retrain the model from the script.
  • The output will now be a ClassificationKNN model that you can call saveCompactModel on.
  • Then from the Simulink function:
function c = useModel(x)
persistent model
if isempty(model)
model = loadCompactModel('savedCompactKNN.mat');
end
c = predict(model, x)
end
You'll notice that I don't need coder.extrinsic as this will generate code.
If you did want to reuse the already trained model, you'll need to grab that property of the trainedClassifier struct and save it:
saveCompactModel(trainedClassifier.ClassificationKNN) % or similar
But then you'll need to map all of the inputs to it which may not be as straight forward as just retraining it with the fitcknn fucntion directly.
Feel free to private message me if you'd like me to walk you through this. I'm trying to find users to convince dev this should be easier.
  댓글 수: 5
Yougzz
Yougzz 2019년 5월 9일
Yes, I checked, It was the problem.
Le Truong An
Le Truong An 2019년 7월 12일
I have the same problem. I am using 2017a.
Do you have any way to solve this problem?

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

추가 답변 (1개)

Tugce Anliak
Tugce Anliak 2019년 7월 9일
Hi i have same issue with a regressionlearner Model that i trained with the App --> saved Model or compact model and try to use MatlabFunction in Simulink and get Errors.
function y = fcn(x1,x2,x3,x4,x5)
y = trainedModel08072019.predictFcn(x1,x2,x3,x4,x5);
This is the Error I get from Matlab:
Undefined function or variable 'trainedModel08072019'.
Function 'MATLAB Function' (#128.38.58), line 3, column 5:
"trainedModel08072019"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'TrainedModel_test/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'TrainedModel_test/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'TrainedModel_test/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Model error
Error occurred in 'TrainedModel_test/MATLAB Function'.
Component:Simulink | Category:Model error
How is the correct form to call a simulink function

카테고리

Help CenterFile Exchange에서 Multicore Processor Targets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by