How to Use Classification in Simulink for Data Science
To use
classification in Simulink, import or create a trained classification model in MATLAB, then integrate it using the MATLAB Function block or Predict block from the Statistics and Machine Learning Toolbox. Connect your input data signals to the block and output the predicted class labels for real-time classification.Syntax
In Simulink, classification is typically done by embedding a trained model using the MATLAB Function block or the Predict block. The general syntax inside the MATLAB Function block is:
label = predict(model, inputData);- wheremodelis your trained classification model andinputDatais the feature vector.- The
labeloutput is the predicted class.
This syntax calls the predict function from MATLAB's Statistics and Machine Learning Toolbox.
matlab
function label = fcn(inputData) %#codegen persistent model; if isempty(model) model = coder.load('trainedModel.mat'); end label = predict(model, inputData); end
Example
This example shows how to classify input data in Simulink using a trained classification model saved in MATLAB. The model is loaded once and used to predict class labels for incoming data signals.
matlab
% MATLAB script to create a simple classification model and save it % Create sample data X = [randn(50,2)+1; randn(50,2)-1]; Y = [ones(50,1); zeros(50,1)]; % Train a simple classification tree model = fitctree(X,Y); % Save the model save('trainedModel.mat','model'); % Simulink MATLAB Function block code: function label = fcn(inputData) %#codegen persistent model; if isempty(model) model = coder.load('trainedModel.mat'); end label = predict(model, inputData); end
Output
No direct output; Simulink block outputs predicted class labels for each inputData vector.
Common Pitfalls
- Not loading the model properly: Forgetting to load the trained model inside the MATLAB Function block causes errors.
- Input data shape mismatch: The input feature vector must match the model's expected size.
- Using unsupported functions: Some MATLAB functions are not supported for code generation in Simulink.
- Not setting
#codegendirective: This directive is needed for code generation compatibility.
matlab
function label = fcn_wrong(inputData) % This will fail because model is not persistent and loaded every call load('trainedModel.mat','model'); label = predict(model, inputData); end % Correct way: function label = fcn_right(inputData) %#codegen persistent model; if isempty(model) model = coder.load('trainedModel.mat'); end label = predict(model, inputData); end
Quick Reference
Tips for classification in Simulink:
- Train and save your model in MATLAB before using it in Simulink.
- Use
persistentvariables to load the model once inside MATLAB Function blocks. - Ensure input data matches the model's expected features.
- Use
#codegendirective for compatibility. - Test your model in MATLAB before integrating into Simulink.
Key Takeaways
Load your trained classification model once using persistent variables inside MATLAB Function blocks.
Use the predict function to classify input data signals in Simulink.
Ensure input data shape matches the model's expected features to avoid errors.
Include the #codegen directive for code generation compatibility.
Test your classification model in MATLAB before Simulink integration.