필터 지우기
필터 지우기

How to load a trained classifier model inside Simulink MATLAB function in MATLAB 2015a?

조회 수: 1 (최근 30일)
Hi,
I have trained a classifier for my dataset in MATLB 2022a and saved it as a mat file (classifiername.mat). I need to load this classifier model inside Simulink Matlab function (MATLAb 2015a) and then use predict() to do prediction for my new data set. When I use these commanda in command window or m.file (not inside Simulink) of MATLAB 2015a
load classifiername.mat
[label] = predict(classifiername,XTest);
It load the classider as an object and everything works well, But I need to do all of these steps in Simulink which doesn't work.
function label = svmIonospherePredict(XTest)
load classifiername.mat ;
[label] = predict(classifiername,XTest);
end
This is the error:
The output of a call to 'load' is not assigned to a variable. Assign its output to a variable without subscripting.
Function 'MATLAB Function' (#35.570.591), line 11, column 1:
"load('classifiername.mat')"
Launch diagnostic report.
when I load the classifier to a variable, it change its format to structure and then I cannot use the model inside the predict () function.
I would be thankful for any help.

채택된 답변

Andrew Ouellette
Andrew Ouellette 2022년 11월 10일
Hi Roya,
As you have just discovered, there are a few restrictions on the MATLAB Function block that are not present in the MATLAB Command Window.
For one, the MATLAB Function block requires knowledge of how many variables will be in its workspace when your model is compiled. Since a .mat file can contain potentially many variables, you must specify which variables you want to load.
From your description, you have two options: either load the data in as a struct or explictly load the variable you want.
Here is an example of how to do the first option:
myStruct = load('classifiername.mat');
classifiername = myStruct.classifiername;
label = predict(classifiername,XTest);
Here is an example of how to do the second option:
classifiername = load('classifiername.mat','classifiername');
label = predict(classifiername,XTest);
You can read more about loading variables at the following documentation page:
  댓글 수: 3
Andrew Ouellette
Andrew Ouellette 2022년 11월 10일
Hi Roya,
Thank you for following up on this- the issue was a bit more complicated than I originally thought.
The issue you are facing now is that the ClassificationDiscriminant class is not supported for code generation, which is something that the MATLAB Function does automatically.
Fortunately, this behavior can be circumvented by using a wrapper function. You can create a script called myFunc that does the actual work, and call this function within the MATLAB Function block by using the "coder.extrinsic" function. You will need to initialize your output with the correct size and data type before calling your wrapped function. Here is an example code for the MATLAB Function block:
function y = fcn(u)
coder.extrinsic('myFunc');
y = 0;
y = myfunc(u);
end
And here is an example wrapped function myFunc:
function y = myFunc(u)
myStruct = load('classifiername.mat');
classifiername = myStruct.classifiername;
y = predict(classifiername,u);
end
Roya Salehzadeh
Roya Salehzadeh 2022년 11월 10일
Hi Andrew,
Thank you so much for the help! It worked, Thanks :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Code Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by