Compare Files and Folders and Merge Files

Compare Files and Folders

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.

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:

  • Current Folder browser — Select a file or folder, right-click, and select Compare Against. Browse to select a second item to compare, or drag and drop a file or folder from your file browser.

    You also can select two files or subfolders, 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 autopopulates the first file for comparison with the current open file. Browse to select a second item to compare, or drag and drop a file or folder from your file browser.

  • MATLAB® desktop — Go to the Home tab, and in the File section, click Compare. Browse to select items to compare, or drag and drop the files or folders from your file browser.

  • 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.

In addition to selecting two files and folder, you also can compare a file with a past version of itself. To compare a file with the autosave version or the saved version on disk, click Compare and select Compare with Version on Disk or Save and Compare with Autosave. For more information, see Compare Files with Autosave Version or Version on Disk. These options are not available for live scripts or functions.

Choose a Comparison Type

The Comparison tool compares selected items using the default comparison type defined for the 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 and then select from the options in the Comparison type menu. The Comparison tool only displays 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.

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 MAT-files to compare, you get the default comparison type showing information about the variables. To change the comparison type to binary, create a new comparison using the Comparison tool.

Explore the Comparison Report

You can explore the comparison report in different ways depending on the items being compared:

  • Folders, zip files, and Simulink® manifests — Compare any combination of folders, zip files, or Simulink manifests to determine which file and folder names are unique to each list. You also can identify whether files and folders with the same name have the same content. For more information, see Compare Folders, Zip Files, and Simulink Manifests.

  • Text — Compare and merge lines in two text files (some other applications refer to this as a file diff operation). For more information, see Compare and Merge Text.

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

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

  • Binary files — Determine whether the contents of two binary files match. For more information, see Compare Binary Files.

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

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

Compare Folders, Zip Files, and Simulink Manifests

You can perform file list comparisons for any combinations of folders, zip files, and Simulink Manifests. For example, you can compare the contents of a folder to the contents of a zip file.

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.

Select Files or Folders to Compare

To select items to compare, see Select the Files or Folders to Compare. When selecting items to compare, include subfolder content in the comparison report by selecting Include subfolders.

Explore Differences

When you perform any file list comparison, a new window opens and displays the contents of the specified lists side by side. This image shows an example comparison report for a two folder comparison.

The Comparison tool highlights files and subfolders that do not match as described in this table.

Difference SummaryHighlight ColorDescription
Contents changedPurpleThe contents of the files or folders differ. Click the compare link to investigate.
AddedBlue The file or folder only exists in the right list.
RemovedYellowThe file or folder only exists in the left list.
IdenticalNoneThe 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.

For information about how to further configure the comparison report, see Comparison Report Tools and Options.

Filter the Report

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.

Compare and Merge Text

You can compare and merge lines in two text files using the Comparison tool.

Select Files or Folders to Compare

To select the files to compare, see Select the Files or Folders to Compare. By default, XML files are compared using a text comparison. To compare XML files with a hierarchical comparison instead, see Compare XML Files.

When you perform a text comparison, a new window opens and displays the two files side by side. Symbols in between the two displayed files indicate how you can adjust the files to make them match. For example, run this code to view a text comparison of the example files lengthofline.m and lengthofline2.m.

file1 = fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m')
file2 = fullfile(matlabroot,'help',...
'techdoc','matlab_env','examples','lengthofline2.m')
visdiff(file1,file2)
The Comparison tool displays the resulting report.

Explore Differences

The Comparison tool highlights the lines that have changed as described in this table.

Highlight ColorDescription
PurpleThe content of the lines differs between the two files. The Comparison tool displays an x between the two lines.
Dark PurpleHighlighted characters are different between the two lines.
BlueLine exists only in right file. The Comparison tool displays a > between the two lines.
YellowLine exists only in left file. The Comparison tool displays a < between the two lines.

The Comparison tool attempts to match lines and detects text that is added, deleted, or changed. It does not do a simple line-by-line comparison. 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 23) in blue. Also, notice that 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.

If the files you are comparing are extremely long, the tool could run out of memory while attempting to perform the file comparison. If it does, it displays this message:

Maximum file length exceeded. 
Defaulting to line-by-line comparison.
When this occurs, the Comparison tool switches to a line-by-line comparison. In a line-by-line comparison, the tool highlights the lines containing the end statement because in performing this operation, it finds that the last line in one file does not match the last line in the other file.

You further can explore and configure the results using several methods:

  • View a summary of differences — To view a summary of differences between two text files, including the number of matching lines and the number of unmatched lines in each file, scroll to the bottom of the comparison report.

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

  • Show differences only — To specify whether to show only differences or the entire files, go to the View tab, click the Filter button, and select or clear Show Differences Only. Showing differences only is especially useful in large text comparison reports. When you are showing differences only and some lines are hidden, the report displays a message indicating the number of lines hidden. For example, if 10 lines are hidden, the report displays 10 unmodified lines hidden.

  • Change the display width — To increase or decrease the line lengths of the text files in the comparison display, go to the View tab, and in the Display section, change the Column Width. Resize the window if necessary.

  • Save HTML Report — To save a copy of the comparison report as an HTML file, go to the Comparison tab, and in the Comparison section, click Save As > HTML.

Step Through Differences

If a text file is lengthy, use the and buttons to step through the results one difference at a time. Once the last difference has been reached, clicking the button loops back to the first difference in the files. Similarly, if the first difference is selected and the button is clicked, the Comparison tool loops to the last difference in the files.

Merge Differences

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.

Tip

You can only merge from left to right. If you want to merge into the left file, use Swap Sides before you start merging. Swap Sides reverts any merges already made and creates a new comparison report for the original files.

To merge a difference from the left file to the right file, select the difference in the report and on the Comparison tab, click the Merge button. The Comparison tool highlights merged lines in gray and displays a green merge arrow.

The merged file name at the top of the report indicates that the file contains unsaved changes (filename.m*).

To revert the last merge operation, on the Comparison tab, click Undo. Click Redo to reapply a merge. To revert all merges and start again, click Refresh.

To save your changes, click Save Merged File. To save to a different name, select Save Merged File > Save Merged File As.

To view the files in the Editor, click a line number link in the report.

Tip

Save your merge changes from the comparison report before making any changes in the Editor, otherwise the comparison report can become incorrect. The report does not update to reflect changes you make in the Editor.

Compare Files with Autosave Version or Version on Disk

You can choose to compare a file open in the Editor with an autosaved or saved version of the file.

To compare the latest version of a file with the Editor's backup copy, first open the file in the Editor. Then, go to the Editor tab, click Compare , and select Save and Compare with Autosave. If your file is modified, the Editor saves the file before comparing. For more information, see Backing Up Files.

To compare an open file that has unsaved changes to the saved version, go to the Editor tab, click Compare , and select Compare with Version on Disk.

Note

If MATLAB automatically saves changes to a file when you click away from the Editor, Compare with Version on Disk and Save and Compare with Autosave are disabled.

To enable Compare with Version on Disk and Save and Compare with Autosave, on the Home tab, in the Environment section, click Preferences, select Editor/Debugger, and uncheck Save changes upon clicking away from a file.

Compare and Merge Live Code

Create Live Code Comparison Report

You can compare and merge lines in two live code files using the Comparison tool. To select the files to compare, see Select the Files or Folders to Compare.

When you perform a live code comparison, a new window opens and displays the two files side by side. For example, suppose you have two files averageweight.mlx and averageweight2.mlx with several differences. When you compare the two files, the Comparison tool displays the resulting report.

Explore Differences

The Comparison tool displays the total number of differences in the bottom right corner of the comparison report, and highlights the lines that do not match as described in this table.

Highlight ColorDescription
PurpleThe content of the lines differs between the two files.
Dark PurpleHighlighted characters are different between the two files.
BlueLine exists only in right file (insertion in right file).The corresponding line in the left file is highlighted using a gray striped pattern.
YellowLine exists only in left file (deletion in right file).The corresponding line in the right file is highlighted using a gray striped pattern.

When comparing live code files, the Comparison tool detects differences in code, text, and text formatting. The Comparison tool ignores output, even if the file contains saved output. If the tool detects a difference within an equation, image, or control, the entire item is highlighted.

The Comparison tool attempts to match lines and detects code and text that is added, deleted, or changed. It does not do a simple line-by-line comparison. For example, in the live code comparison of averageweight.mlx and averageweight2.mlx, the tool determines that averageweight.mlx has two lines of code that do not exist in averageweight2.mlx and highlights them (line 5 and line 7) in yellow. Also, notice that the tool takes the additional lines into account and determines that the line containing the disp statement in each file matches, even though the disp statement does not occur on the same line number.

If a live code file is lengthy, use the Next and Previous buttons to step through the results one difference at a time.

Merge Differences

When comparing live code files, you can merge changes from one file to the other. Merging changes can be useful when resolving conflicts between different versions of files. 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 button located next to the line you want to merge. You also can select a difference and in the Merge 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 (filename_modified.mlx*) indicates that the file contains unsaved changes.

To undo a replacement, click the 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 in the Live Editor. Refreshing discards all 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.

Compare and Merge MAT-Files

Create MAT-File Comparison Report

You can use the Comparison tool to compare two MAT-files. Compare two MAT-files to:

  • Determine which variables are common to each file and which are unique.

  • Identify differences between individual variables. For example, you can determine which fields of a structure are different, or which elements of a array differ.

To select items to compare, see Select the Files or Folders to Compare.

When you perform a MAT-file comparison, a new window opens and displays the two files side by side. For example, supposed you have two MAT-files, data1.mat and data2.mat. When you compare the two files, the Comparison tool displays the resulting report.

Explore Differences

The Comparison tool report highlights changes in variables as described in this table.

Difference SummaryHighlightingDescription
ModifiedPurpleValues of the variable differ between the two files. Click the compare link to investigate. For more information, see Compare Variables.
AddedBlueThe variable only exists in the right file.
RemovedYellowThe variable only exists in the left file.
EquivalentNoneThe variables in both files are equivalent. The tool ignores differences in NaN patterns, field ordering in structs, and the difference between negative zero or positive zero.
Class changedPurple (only in Class columns)The variable data class changed. Click the compare link to investigate.

The report displays a message if the variables in both files are equivalent but the files are not identical. Possible causes of the differences between the files include file formats, file timestamps, the order in which the variables are stored, or variables that contain ignored differences.

You can further explore the results in the report using several methods:

  • Sort results — To sort the results by name, size, class, or difference summary, click the corresponding column header. For example, click the Class column header to sort by class type.

  • Load variable — To load the contents of a variable into the Variable Editor, click the name of that variable.

  • Load MAT-file — To load one of the MAT-files into the workspace, click the corresponding Load link at the bottom of the report.

  • Save HTML report — To save a copy of the report as an HTML file, go to the Comparison tab and click Save As > HTML.

For information about how to further configure the comparison report, see Comparison Report Tools and Options.

Compare Variables

The variable comparison report displays differences in individual array elements or fields of a structure. To investigate individual differences, double-click a highlighted row.

Merge Differences

When comparing MAT-files, you can merge changes from one file to the other. There are no undo capabilities when merging MAT-files, so merge with caution.

To copy modified variables from one file to the other, on the Comparison tab, in the Merge section, click the Merge button.

Compare Binary Files

Create Binary Comparison Report

You can compare two binary files such as DLL files or MEX-files using the Comparison tool. To select the files to compare, see Select the Files or Folders to Compare.

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, set the Comparison type to Binary comparison. For more information, see Compare Binary Files.

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.

Comparison Report Tools and Options

You can further explore and configure a comparison report using the available tools and options.

  • Exchange the Left and Right Sides of the Report — To switch the left side file or folder with the right side file or folder, go to the Comparison tab and click the Swap Sides button.

  • Refresh the Report to Show Updated File — After making changes to and saving the files in the Editor, to update the results in the Comparison tool, go to the Comparison tab and click the Refresh button.

  • Find Text — To find a phrase in the current display, go to the Comparison tab and click the Find button. For more information, see Find Text in Command Window or History.

  • Change Color Preferences — To change and save the color preferences for the Comparison tool:

    1. On the MATLAB Home tab, click Preferences and select MATLAB > Comparison.

    2. Select the desired colors for differences, modified lines, modified contents, and merged lines. If you change a color preference, Active Settings displays Default (modified).

    3. Click Apply and refresh the comparison report to see the updated colors reflected in the report.

    4. To save the modified color preferences for use in future MATLAB sessions, click Save As. Enter a name for your color settings profile and click OK.

      After saving a color settings profile, you can select the profile in the Active Settings list.

    5. To restore the default settings, click Reset and then click Apply. Refresh the comparison report.

    Color preferences apply to all comparison types.

Source Control

You can customize external source control tools to use the MATLAB Comparison tool for comparing and merging files and folders. For more information, see Customize External Source Control to Use MATLAB for Diff and Merge.

Troubleshooting

If you encounter errors like java.lang.OutOfMemoryError: Java heap space or java.lang.OutOfMemoryError: GC Overhead limit exceeded when comparing large files, then edit your MATLAB preferences to increase the heap size.

  1. On the Home tab, in the Environment section, click Preferences.

  2. Select MATLAB > General > Java Heap Memory.

  3. Move the slider to increase the heap size, and then click OK.

  4. Restart MATLAB.

See Also

Related Topics