Compare Files and Folders and Merge Files - MATLAB & Simulink (original) (raw)

Use the Comparison Tool to display the differences between selected pairs of files or folders.

You can select files and folders on disk to compare using one of these methods:

For files under source control, you can open the Comparison Tool to understand differences between revisions using one of the following methods.

The Comparison Tool compares selected items using the default comparison type. To specify a different comparison type, use the visdiff. For example, you might want to change the comparison type from"text" to "binary" to examine differences such as end-of-line characters in text files.

When you select folders to compare, by default, the Comparison Tool includes the content of all subfolders recursively in the comparison. To include only the folders you select and the first-level subfolders in the comparison report, in MATLAB, enter the following commands.

s=settings(); s.comparisons.folder.IncludeSubfolders.TemporaryValue=false;

Compare Folders and Zip Files

You can compare any combination of folders and ZIP files using the Comparison Tool. For example, you can compare the contents of a folder to the contents of a ZIP file. The Comparison Tool performs a file list comparison of the selected items.

Tip

Starting in R2025a, when you compare folders, MATLAB detects whether they are project root folders. The Comparison Tool opens a project definition files comparison report. For more information, see Compare MATLAB Projects.

Create a folder comparison report to:

When you perform a file list comparison, a new window opens and displays the contents of the specified lists side by side. For example, suppose that you have two folders,curvefitting and curvefitting2, with several differences. When you compare the two folders, the Comparison Tool displays the resulting report.

Folder comparison results for the curvefitting and curvefitting2 folders

The Comparison Tool highlights files and subfolders that do not match using the colors listed in this table.

Highlight Color Description
Purple The contents of the files or folders differ. Click thecompare link to investigate.
Blue The file or folder only exists in the right list.
Yellow The file or folder only exists in the left list.
None The files or folders are identical.

You further can explore the results using several methods:

You can further configure and explore the comparison report using the following methods:

Compare Text Files

You can compare and merge lines in two text files using the Comparison Tool. When you perform a text comparison, a new window opens and displays the two files side by side.

Compare Files

A user made some changes to lengthofline.m and saved the resulting file as lengthofline2.m.

Use visdiff to compare the lengthofline.m and lengthofline2.m files.

visdiff("lengthofline.m","lengthofline2.m")

The Comparison Tool displays the resulting report.

Comparison results for the files lengthofline.m and lengthofline2.m

Understand Comparison Results

The Comparison Tool displays the total number of differences in the bottom-right corner of the comparison report and highlights the lines that have changed with the colors listed in this table.

If the files are identical or differ only in line break, you see a message reporting there are no differences.

Default Highlight Color Description
Purple The line contains differences.
Dark purple The boxed text is different.
Blue The line exists only in the right file. This can happen when the line is inserted in the right file, or when it is deleted from the left file. The corresponding line in the left file is highlighted using a gray striped pattern.
Yellow The line exists only in the left file. This can happen when the line is inserted in the left file, or when it is deleted from the right file. The corresponding line in the right file is highlighted using a gray striped pattern.

The Comparison Tool attempts to match lines and detects text that is added, deleted, or changed. For example, in the text comparison oflengthofline.m and lengthofline2.m, the tool determines that lengthofline2.m has a line of code that does not exist in lengthofline.m and highlights it (line 22) in blue. Also, the tool takes the additional line into account and determines that the line containing the end statement in each file matches, even though the end statement does not occur on the same line number in both files.

You can further configure and explore the comparison report using the following methods:

Merge Text Files

When comparing text files, you can merge changes from one file to the other. Merging changes can be useful when resolving conflicts between different versions of files. When merging changes, you only can merge from left to right. If you want to merge into the left file, click Swap Sides before you start merging. Swapping sides reverts any merges already made and creates a new comparison report from the original files.

To begin merging, in the Comparison Tool toolstrip, click Merge Mode. Then, to replace content from the right pane with content from the left pane, click the Replace Content button Replace Content button located next to the line you want to merge. Alternatively, select a difference and, in the Comparison Tool toolstrip, clickReplace Content.

The right pane contains the merged result. An asterisk next to the merged file name in the right pane (lengthofline2.m *) indicates that the file contains unsaved changes.

To undo a replacement, click the Undo button between the modified lines or in the toolstrip. To revert all merges and start again, in the Comparison Tool toolstrip, clickRefresh. You can also use Refresh to update the comparison report after making changes to and saving the files. Refreshing discards all unsaved merged changes.

To save your changes and return to the comparison report, in the toolstrip, clickSave Result button. To return to the comparison report without saving changes, click Return to Comparison.

Comparison Tool in merge mode for the files lengthofline.m and lengthofline2.m

Compare Binary Files

You can compare two binary files, such as DLL files or MEX files, using the Comparison Tool. You also can perform a binary comparison on any two selected files, instead of the default comparison.

To compare any two files using a binary comparison, in the Comparison type menu, select Binary comparison. When you perform a binary comparison, a new window opens and indicates whether the two files are identical or different. If the files are different, click the Show Details link to view the binary files and the byte offset of the first difference.

Compare Other File Types

You can use the Comparison Tool to compare other file types:

Comparison Settings

You can customize the colors that the Comparison Tool uses in the Comparison settings. To change the Comparison settings, on the Home tab, in theEnvironment section, click Settings. Then, select > .

Color settings apply to all comparison and merge types. After changing the colors, any open comparison and merge reports reflect the updated colors. MATLAB remembers your choices for future sessions. You can define a set of colors per desktop theme. To restore default colors, click Restore Default Colors.

This table provides a summary of the colors that the comparison and merge reports use.

Color of report item Description
Modified/Theirs In the comparison report, the color is associated to a modified line or item.
In the three-way merge report, the color is associated to a change made by the Theirs revision.
Deleted/Base In the comparison report, the color is associated to a deleted line or item.
In the two-way merge report, the color is associated to a change made by the Left revision.
In the three-way merge report, the color is associated to a change that exists in the Base revision.
Inserted/Mine In the comparison report, the color is associated to an added line or item.
In the two-way merge report, the color is associated to a change made by the Right revision.
In the three-way merge report, the color is associated to a change made by the Mine revision.
Target In the two-way and three-way merge reports, the color is associated to a change made in the Target pane.
Conflict In the three-way merge reports, the color is associated to a line that contains conflicts.
Modified/Theirs Margin In the comparison report, the color is associated to the margin of a modified line or item.
In the three-way merge report, the color is associated to the margin of a change made by the Theirs revision.
Deleted/Base Margin In the comparison report, the color is associated to the margin of a deleted line or item.
In the two-way merge report, the color is associated to the margin of a change made by the Left revision.
In the three-way merge report, the color is associated to the margin of a change that exists in the Base revision.
Inserted/Mine Margin In the comparison report, the color is associated to the margin of an added line or item.
In the two-way merge report, the color is associated to the margin of a change made by the Right revision.
In the three-way merge report, the color is associated to the margin of a change made by the Mine revision.
Target Margin In the two-way and three-way merge reports, the color is associated to the margin of a change made in the Target pane.
Conflict Margin In the three-way merge reports, the color is associated to the margin of a line that contains conflicts.
Inline Modification In the comparison report, the color is associated to a inline modification.

Note

The margin of a node in the report may display more than one color. For example, when a node has both the blue and purple colors, it indicates that the detailed comparison of this node contains both added and modified items. To open the details pane, select the node.

The following illustration shows a side by side comparison example. The margin of the parent node MATLAB Function, on the right, is colored in both dark purple and dark blue to indicate both modified and added items in the detailedScript comparison below. In the detailedScript comparison, the modified lines and items are colored in light purple and the corresponding margins are colored in dark purple. The added line is colored in light blue and the corresponding margin is colored in dark blue. The deleted line is colored in light orange and the corresponding margin is colored in brown.

For more information about the terminology used in the Comparison and Merge tools, seeTerminology in Comparison Report (Simulink) and Terminology in Three-Way Merge Report (Simulink).

See Also

visdiff

Topics

External Websites