‹ SimDiff Support

3-way Diff and Merge of Simulink Models: A Step-by-Step Guide

A three-way comparison uses the common ancestor, so SimDiff can tell an addition from a deletion before you merge.

A 3-way comparison and merge tool matters when several people change the same Simulink model in a version control system.

Why version control needs a 3-way comparison

In a team, several people often change the same model. A three-way comparison resolves conflicts by comparing two modified versions of a model against their common ancestor.

See how to compare two Simulink models, or continue here for a three-way comparison.

A 3-way diff is more exact than a 2-way diff because it tells you how the models were changed, not just how they differ. Suppose two versions were created by changing a common ancestor. A 2-way diff can only tell whether two elements are the same or different. In the table below, without the original model there is no way to know whether D was added to Model 2 or deleted from Model 1.

Table of Model 1 and Model 2 listing blocks A, B1 and B2, C, D, and E, each marked Same or Different

Now suppose the original model, the common ancestor, contained only blocks A, B, and C.

Table of the original model listing blocks A, B, and C

A 3-way diff examines the original model along with the two models to compare, and it can indicate how the models were changed.

The same Model 1 and Model 2 table, now marked as a conflict, a deletion in Model 2, an addition in Model 2, and an addition in Model 1

You can see which changes were additions and which were deletions, not just how Model 1 and Model 2 differ.

Version control systems assume an accurate 3-way diff and merge tool is available. Simulink is a proprietary format, so a version control system’s built-in 3-way diff and merge will not support it. If you work at an automotive, aerospace, or defense company, chances are you already have SimDiff, which computes the 3-way diff and assists with the 3-way merge. If not, request a SimDiff trial.

Simple 3-way merge

How to perform a 3-way merge without version control integration

Typically you launch SimDiff from the version control client. The version control setup guides show how. You can also run a 3-way merge from SimDiff itself.

  1. Open SimDiff.
  2. Select Merge mode.
  3. Use the browse button or drag and drop the files into the left, right, and base model fields.
  4. Click the Start button.
SimDiff start window in Merge mode, with left, right, and base aero_radmod models selected and the merge output set to the left model

SimDiff compares the models and shows both model hierarchies with the differences highlighted. Each difference is numbered and colored. By default, SimDiff uses blue for changes, red for deletions, and green for additions.

When you click a changed element, individual attribute differences appear in the Attributes view at the bottom.

SimDiff three-way comparison with a changed element selected and its attribute differences listed in the Attributes view

SimDiff provides different options to make it easy to navigate through the differences.

By default, it shows subsystems at the top level collapsed. Select the top system and use the Expand-all button to expand every subsystem underneath, then scroll the model. This can be useful with small models.

SimDiff model tree with the Expand all button and the root system selected, showing collapsed subsystems underneath

Other options make large models easier to navigate. Use the navigation toolbar buttons to locate the next or previous difference, and the next or previous conflict.

SimDiff toolbar buttons for the next and previous difference and the next and previous conflict

You can also search for an element. The model tree filters to elements that match the search term. Use the inner navigation buttons to step through the matches.

SimDiff search box filtering the model tree to elements that match the search term

By default, SimDiff shows the whole model, including unchanged elements. Switch to a view of only the differences by clicking the All Changes button. After a 3-way comparison, you can also filter the view to changes on the left model or the right model.

SimDiff toolbar with All Changes selected on a three-way comparison, showing only the differences in the model tree

SimDiff integrates with the Simulink Editor. You can view model changes side by side in their graphical context. Double-click any element in SimDiff’s tree view and Simulink locates and highlights it.

SimDiff beside Simulink, with a changed block highlighted in the model editor

Merging differences

SimDiff provides different options to merge differences. The easiest way to merge a single difference is to click the merge action icon in the middle column between the two model hierarchies.

To merge several changes at once, select the changes and click the Merge selected elements button in the toolbar.

SimDiff toolbar with Merge selected elements, and merge action icons in the column between the two model trees

You can also select any container node, such as a subsystem, and click the hierarchy merge toolbar button. Hierarchy merge automatically merges every difference underneath that does not require user input. After hierarchy merge, review conflicts and Stateflow changes. Layout can affect Stateflow simulation behavior, so those changes need extra care.

SimDiff after hierarchy merge, with the hierarchy merge toolbar button and remaining conflicts still marked

Identifying and resolving conflicts

Use the next or previous conflict toolbar buttons to move through the conflicts. A conflict shows a red merge action icon.

To keep the change from the right model, click the merge action button. Otherwise, the change from the left model remains.