Comparing Structural Differences in Simulink Models Using MATLAB Scripts

In model iteration and team collaboration, quickly identifying structural differences in Simulink models is crucial. This article systematically introduces how to automate model comparison using MATLAB scripts, covering the core technical paths of official tools and custom scripts.

📊 1. Overview of Simulink Model Comparison

Model comparison focuses on structural differences (module additions/deletions, parameter changes, connection adjustments) and behavioral differences (output response consistency). This article emphasizes automated comparison methods for structural differences, suitable for continuous integration (CI) processes or batch processing scenarios.

⚙️ 2. Efficient Use of Built-in Comparison Tools

1. Quick Start and Operation

matlab  

visdiff('model_v1.slx', 'model_v2.slx'); % Launch graphical comparison tool

Features:

– Side-by-side display of models, highlighting differences (blue for additions, yellow for deletions, purple for modifications);

– Supports HTML/Word/PDF report generation;

– Merge mode allows selective application of differences.

2. Applicable Scenarios

– Temporary Comparison: Quickly locate modifications in small-scale models;

– Teaching Demonstration: Visually present model change logic.

📜 3. Custom Scripts for In-Depth Comparison

1. Core Process and Key Functions

Flowchart:

Start → Select Model → Create Temporary Copy → Load Model → Compare Differences → Generate Report → Clean Temporary Files → End

Key Functions:

– Simulink.compareModels: Obtain difference information object;

– find_system / get_param: Traverse modules and parameters;

– mlreportgen.dom: Generate PDF reports.

2. Script Framework and Code Example

matlab  

% Select and copy model to temporary directory
oldPath = uigetfile('*.slx', 'Original Model');
newPath = uigetfile('*.slx', 'Modified Model');
tempDir = fullfile(pwd, 'temp_diff');
mkdir(tempDir);
copyfile(oldPath, fullfile(tempDir, 'old.slx'));
copyfile(newPath, fullfile(tempDir, 'new.slx'));
% Load and compare models
oldModel = load_system(fullfile(tempDir, 'old.slx'));
newModel = load_system(fullfile(tempDir, 'new.slx'));
compResult = Simulink.compareModels(oldModel, newModel);
diffItems = compResult.getDifferences();
% Generate PDF report
import mlreportgen.dom.*;
doc = Document('diff_report', 'pdf');
append(doc, Heading(1, 'Model Difference Report'));
for i = 1:numel(diffItems)
    diff = diffItems(i);
    if ~any(strcmp(diff.Type, {'Block', 'Line', 'Annotation'}))
        continue; % Filter non-key differences
    end
    append(doc, Heading(2, ['Difference Item #', num2str(i)]));
    append(doc, Paragraph(['Path: ', diff.Path2]));
    append(doc, Paragraph(['Type: ', diff.Type]));
    append(doc, Paragraph(['Description: ', get_diff_description(diff)]));
end
close(doc);
close_system(oldModel);
close_system(newModel);
rmdir(tempDir, 's');

3. Handling Difference Types

– Module Differences: Additions, deletions, or parameter modifications (e.g., gain value changes);

– Connection Differences: Adjustments in signal connection relationships;

– Annotation Differences: Changes in text content or position.

⚠️ 4. Considerations and Optimization Suggestions

1. Model Loading Strategy:

– Use temporary copies to avoid memory naming conflicts;

– close_system(model, 0) to silently close models.

2. Performance Optimization:

– Increase wait time for large-scale models;

– Filter irrelevant differences (e.g., module position movements).

3. Version Compatibility:

– Ensure MATLAB version ≥ R2012b, avoid using deprecated methods.

4. Result Validation:

– Manually review automatically generated reports and screenshots.

💡 5. Extended Application: Behavioral Difference Comparison

Script Idea:

matlab  

% Generate the same input signal
t = 0:0.01:10;
u = sin(t);
% Simulate and compare outputs
sim('model_v1', t);
sim('model_v2', t);
% Calculate difference metrics
mse = mean((yout1 - yout2).^2);
max_err = max(abs(yout1 - yout2));

🌟 Summary: Technical Solution Selection Guide

Scenario Tool/Method Advantages

Quick Interactive Comparison visdiff Graphical Tool Intuitive and easy to use, suitable for small-scale models

Automated CI/CD Integration Custom Script + Simulink.compareModels Batch processing, generates custom reports

Behavioral Difference Analysis Script Simulation + Output Comparison Quantitative assessment of model behavior consistency

By combining official tools and custom scripts, an efficient model difference management system can be built, significantly improving development efficiency and code quality.

Leave a Comment