How to include a trained Classification SVM model into Simulink model

6 visualizzazioni (ultimi 30 giorni)
  1. I have trained a SVM model in Classification Learner app.
  2. I am trying to include this trained SVM model into a simulink model using MATLAB function block.
  3. My Matlab function block code is as below:
function label = CSVMPredict(X)%#codegen
%svm
Mdl = loadLearnerForCoder('CSVM_LOC');
[label,bothscores] = predict(Mdl,X);
end
4. In the above code, 'CSVM_LOC.mat' is extracted from the trained SVM Classification Learner app model using saveLearnerForCoder command.
5. I am getting the following error upon implementation:
A top-level output parameter containing a categorical is not supported in MATLAB Function Block. Output parameter 'label' contains a categorical.
6. Request a solution for this problem from the community.

Risposte (1)

Subhajyoti Halder
Subhajyoti Halder il 21 Giu 2023
Hi Amit,
The error message you are getting suggests that the output `label` of your MATLAB function block is a categorical data type, which is not supported as a top-level output parameter in Simulink. Simulink only supports certain MATLAB data types, such as scalars, vectors, and matrices of numeric, logical, or enumerated data types.
To resolve this issue, you can convert the categorical `label` output to a numerical or logical data type in your MATLAB function block, which can then be used as a top-level output in Simulink.
One possible way to do this is to use the `double` function to convert the categorical values to doubles or use the `cell2mat` function to convert the categorical values to a cell array of strings and then use `strcmp` or `strcmpi` to compare the strings.
Here is an example implementation:
function label_num = CSVMPredict(X) %#codegen
% Load the trained SVM model
Mdl = loadLearnerForCoder('CSVM_LOC.mat');
% Make a prediction using the trained SVM model
[label, bothscores] = predict(Mdl,X);
% Convert categorical label to a numeric vector
label_num = double(label);
end
In this example, the `double` function is used to convert the categorical `label` output to a numeric vector `label_num`. Make sure to test your implementation thoroughly to ensure that the conversion does not introduce errors in the results.
For further details on model configuration parameters: data import/export, kindly go through the following documentation:

Community Treasure Hunt

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

Start Hunting!

Translated by