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

4 views (last 30 days)
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.

Accepted Answer

Andrew Ouellette
Andrew Ouellette on 10 Nov 2022
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 Comments
Andrew Ouellette
Andrew Ouellette on 10 Nov 2022
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!