Main Content

Compare Files and Folders and Merge Files

You can use the Comparison Tool to display the differences between selected pairs of files or folders. For some file types, you also can merge changes from one file to the other.

Comparison Process

The comparison process involves three steps:

  1. Select the files or folders to compare.

  2. Choose a comparison type.

  3. Explore the comparison report.

Select the Files or Folders to Compare

You can select files and folders to compare using any of these methods:

  • MATLAB® desktop — Go to the Home tab, and in the File section, click Compare. Click the Browse to select File or Folder button to select items to compare, or drag and drop files from your file browser into the First file or folder or Second file or folder fields.

  • Current Folder browser — Select a file or folder, right-click, and select Compare Against. Click the Browse to select File or Folder button to select a second item to compare, or drag and drop a file from your file browser into the Second file or folder field. To select two files or subfolders to compare, Ctrl-click the file names. Then right-click and select Compare Selected Files/Folders.

  • Editor or Live Editor — Go to the Editor or Live Editor tab, and in the File section, click Compare. The Comparison Tool includes the currently open file in the First file or folder field. Click the Browse to select File or Folder button to select a second item to compare, or drag and drop a file from your file browser into the Second file or folder field.

    To compare a file with the autosave version or the saved version on disk, go to the Editor tab and click Compare drop-down arrow and select Compare with Version on Disk or Save and Compare with Autosave. If your file is modified, the Editor saves the file before comparing. The Compare with Version on Disk and Save and Compare with Autosave options are only available if the option for automatically saving changes to a file is disabled. To disable the option, go to the Home tab, and in the Environment section, click Preferences. Then, select Editor/Debugger and clear Save changes upon clicking away from a file. This option is not available in the Live Editor.

  • Command Window — Use the visdiff function. For example, to compare the two files lengthofline.m and lengthofline2.m using the visdiff function and the default text comparison, type visdiff('lengthofline.m','lengthofline2.m'). MATLAB opens the Comparison Tool and displays the resulting comparison report.

Tip

When selecting folders to compare, select Include subfolders to include subfolder content in the comparison.

Choose a Comparison Type

The Comparison Tool compares selected items using the default comparison type defined for the selected items. For some item types, you can select a different comparison type from the default. For example, you can choose from a text, binary, file list, or XML comparison. To change the comparison type, select the files or folders to compare. Then, select from the available Comparison type options. The Comparison Tool displays only the valid options for the selected item types.

You cannot change the comparison type for an existing comparison report. To change the comparison type, start a new comparison.

Select Files or Folders for Comparison dialog box with text boxes for specifying the two files or folders to compare and a drop-down list for selecting the comparison type

If you specify two files or folders to compare using either the Current Folder browser or the visdiff function, then the Comparison Tool automatically performs the default comparison type. For example, from the Current Folder browser, if you select two XML files to compare, the tool uses the default text comparison. To change the comparison type to a hierarchical comparison instead, create a new comparison using the Comparison Tool.

Explore the Comparison Report

After selecting the comparison type, click the Compare button. The comparison report opens. You can explore the comparison report in different ways depending on the items being compared. For more information, see the details described for each comparison type below.

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.

Create a folder comparison report to:

  • Identify common files.

  • Determine whether files or folders with identical names have identical content. If such files or folders exist, you can open a detailed comparison report for the items directly from the report.

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 ColorDescription
PurpleThe contents of the files or folders differ. Click the compare link to investigate.
Blue The file or folder only exists in the right list.
YellowThe file or folder only exists in the left list.
NoneThe files or folders are identical.

You further can explore the results using several methods:

  • To sort the results by name, type, size, or last modified timestamp, click the corresponding column header. For example, click the Type column header to sort by folder and file type.

  • To open a detailed comparison report for items with differing content, click the compare link next to the item.

  • To open a file in the Editor, click the open link next to a file name. If the file is present in both folders, you can click links to open the left or right version of the file.

  • If subfolders are very large and contain many files, analysis continues in the background. The tool displays the number of items still to be compared at the top of the report. You can click Skip Current to skip the current item or Cancel All to stop further analysis.

    Section of the comparison report showing the number of items that still need to be compared, a Skip Current hyperlink, and a Cancel All hyperlink

  • To save time when reviewing differences, especially when comparing many subfolders, you can filter the report. To apply a filter, on the View tab, select Filter > "filterName".

    To create a new filter, on the View tab, select Filter > Add/Remove Filter. You can specify filters to ignore certain files and folders, such as backup files or files created by a revision control system. For example, to ignore all files and folders in a folder named CVS, type CVS/. To ignore all files in a folder named CVS, but not ignore subfolders, type CVS/*.

    To edit existing filters, double click the filter.

For information about how to further configure the comparison report, see Additional Comparison Tools.

Compare MATLAB Projects in MATLAB Online

In MATLAB Online™, you can compare project definition files. When you compare folders, MATLAB detects whether they are project root folders. MATLAB looks for and compares the project definition files stored in the resources or .SimulinkProject folder. Project definition files contain information about the project path, project settings, shortcuts, labels, and referenced projects.

To compare project definition files, select the PRJ files or the project root folders, right-click your selection, and select Compare Selected Files/Folders. When you select a node in the results report, the Comparison Tool shows more details in the bottom pane.

Project definition file comparison

This example comparison shows that:

  • AnalogControl.slx (purple) is modified.

  • f14_airframe.slx (yellow) is replaced by f14_airframe.mdl (blue) in the project.

  • LinearActuator.slx is modified (purple) and the model has a new label associated to it (blue).

  • A new label classification category named Release Branch is added.

  • The reference to the Signal Multiplier project is deleted.

  • The F14 Model shortcut is deleted.

  • Project settings such as project details, path, and dependency settings are modified. For example, in the Details section of Project Settings, the project description is modified.

    Project description comparison

You can compare project definition files only in MATLAB Online.

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.

Highlight ColorDescription
PurpleThe line contains differences.
Dark PurpleThe highlighted characters are different.
BlueThe 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.
YellowThe 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 of lengthofline.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 further can explore and configure the results using several methods:

  • Ignore white space differences — To help distinguish between functional changes and changes to indentation, you can hide white space differences. To hide differences that only involve white space characters, click Filter, and select Ignore White Space.

  • Show compared files details — To show the file details, click the expand arrow next to the file name.

  • Save comparison report — To save a copy of the comparison report, select Publish > Publish to HTML, Publish to Word, or Publish to PDF.

For information about how to further configure the comparison report, see Additional Comparison Tools.

To step through the results one difference at a time, use the Next and Previous buttons.

By default, XML files are compared using a text comparison. To compare XML files with a hierarchical comparison instead, see Compare XML Files.

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 the Swap Sides button before you start merging. Clicking the Swap Sides button reverts any merges already made and creates a new comparison report for the original files.

To begin merging, on the Comparison tab, click the Merge Mode button. Then, to replace content from the right pane with content from the left pane, click the Replace Content button located next to the line you want to merge. You also can select a difference and, in the Comparison tab, click the Replace Content button.

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 on the Comparison tab. To revert all merges and start again, click the Refresh button. You also can click the Refresh button 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, click the Save Result button. To return to the comparison report without saving changes, click the Return to Comparison button.

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:

  • Live Code — Compare and merge the code and text in two live code files. For more information, see Compare and Merge Live Scripts and Functions.

  • Apps — Compare and merge the code in two apps. For more information, see Compare and Merge Apps.

  • MAT-Files — Compare and merge variables in two MAT-files. For more information, see Compare and Merge MAT-Files.

  • XML Files — Compare two XML files using a text comparison or a hierarchical XML comparison. For more information, see Compare XML Files.

  • Simulink® Models — If you have Simulink, you can compare and merge Simulink models. For information, see Model Comparison (Simulink).

  • MLDATX test files — Compare and merge Simulink Test™ test cases in MLDATX test files. For information, see Compare and Merge Test Files (Simulink Test)

Additional Comparison Tools

You can further explore and configure a comparison report using these available tools:

  • Swap Sides button — Switch the left side file or folder with the right side file or folder.

  • Refresh button — Update the results in the Comparison Tool after making changes to and saving the files in the Editor.

  • Find button — Find a phrase in the current display. For more information, see Find Text in Command Window or History.

Comparison Preferences

You can customize the Comparison Tool using Comparison preferences. To change the Comparison preferences, on the Home tab, in the Environment section, click Preferences. Then, select MATLAB > Comparison.

Color Preferences

You can change the colors that the Comparison Tool uses to highlight lines that have changed. Color preferences apply to all comparison types.

To change the color preferences, in the Comparison preferences Colors section, change the colors for differences, modified lines, modified content, and merged lines. The Sample area shows a preview of the selected colors. After changing comparison colors, you must refresh any open comparison reports to see the updated colors.

To save the modified color preferences for use in future MATLAB sessions, click Save As and enter a name for your color settings profile. After saving a color profile, you can select the profile in the Active Settings list. To restore the default color profile, click Reset.

External Source Control Integration Preferences

You can use the Comparison Tool to compare and merge files and folders in external source control tools. To enable external source control tools to reuse open MATLAB sessions, in the Comparison preferences External Source Control Integration section, select the Allow external source control tools to use open MATLAB sessions for diffs and merges option. When you configure your source control tool to use the MATLAB Comparison Tool, the Comparison Tool prompts you to select this option.

For more information, see Customize External Source Control to Use MATLAB for Diff and Merge (Simulink).

See Also

Related Topics

External Websites