How to include a trained Classification SVM model into Simulink model
조회 수: 6 (최근 30일)
이전 댓글 표시
- I have trained a SVM model in Classification Learner app.
- I am trying to include this trained SVM model into a simulink model using MATLAB function block.
- 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.
댓글 수: 0
답변 (1개)
Subhajyoti Halder
2023년 6월 21일
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:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Classification Learner App에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!