Skip to Main Content Skip to Search
Product Documentation

Comparing Files and Folders

Comparing Files and Folders

You can use the Comparison Tool to determine and display the differences between selected pairs of files or folders. The comparison process involves three steps:

  1. Select the Files or Folders to Compare

  2. Choose a Comparison Type

  3. Explore the Comparison Tool Report

Select the Files or Folders to Compare

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

If you use Compare Against, select the second item to compare in the Select Files or Folders for Comparison dialog box. Optionally, change the comparison type if multiple types are available for your selections.

Choose a 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.

If there are multiple comparison types available for your selections, you can change what type of comparison to run. For example, text, binary, file list, or XML comparison. To change the comparison type, create a new comparison using the Comparison Tool. You can change comparison type in the Select Files or Folders for Comparison dialog box.

Image of Select Files or Folders for Comparison dialog box with choice of comparison type as described in the text preceding the image.

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. See Selecting Files or Folders to Compare from the Comparison Tool.

Explore the Comparison Tool Report

Comparison Tool report features depend on your comparison type. You can use the tool to:

Comparing Folders and Zip Files

Folder Comparison Report

To select items to compare, see Select the Files or Folders to Compare. You can perform file list comparisons for any combinations of folders, zip files, and Simulink Manifests.

When you use the Comparison Tool to compare two folders (sometimes referred to as directories) or any file list comparison (for example, folder versus zip file), a window opens and presents the contents side by side. The tool enables you to:

For list comparisons, if you want to expand the list to see all files in subfolders in one report, select the Include subfolders check box when selecting items to compare. If you do not include subfolders, you can click compare links in the report to open a new comparison of two folders with changed content.

To define filters to exclude unimportant differences, click the Filters toolbar button, , or select Comparison > Edit File and Folder Filters.

The File and Folder Filters dialog box opens. Specify filters to ignore certain files and folders, such as backup files or files created by a revision control system. Filters can save time when reviewing differences, especially when comparing many subfolders. Double-click to edit existing filters.

For example, to ignore all files and folders in a folder named CVS, open the File and Folder Filter dialog box and enter:

CVS/

To ignore all files in a folder named CVS, but not ignore subfolders, enter:

CVS/*

Highlighting of Differences

The Comparison Tool displays the contents of the lists side by side and highlights files and subfolders that do not match. The following table describes how the tool highlights each type of change. The status message (such as identical or contents changed) appears in the Change Summary column.

Change SummaryHighlighting for FoldersHighlighting for FilesNotes
Contents changedDark pinkPinkThe contents of the files or folders differ. Click the compare link to investigate.
AddedDark greenGreenFile or folder only exists in the right list.
RemovedDark purplePurpleFile or folder only exists in the left list.
IdenticalNoneNone 

The following image shows an example of the Comparison Tool when two folders are compared. The results are sorted by Type.

Image of Comparison Tool window with highlighting as described in the text preceding the image.

Next Steps Using the Report

To explore the report you can use the following tools:

Comparing Text Files

Selecting Text Files to Compare

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

Highlighting of Differences

When you use the Comparison Tool to compare two text files, a window opens and presents the two files side by side. Symbols indicate how you can adjust the files to make them match. This feature can be useful when you want to compare the latest version of a text file to an autosave version.

The Comparison Tool report displays the files side by side and highlights lines that do not match, as follows:

Image of Comparison Tool window with highlighting as described in the text preceding the image.

The Comparison Tool attempts to match lines and detects local text that is added, deleted, or changed. It does not do a simple line-by-line comparison. In the previous image, for example, the tool determines that lengthofline.m has a line of code that does not exist in lengthofline2.m and highlights it (line 23) in green. 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. In which case, the message,

Maximum file length exceeded. 
Defaulting to line-by-line comparison.

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

Stepping Through Differences

Because text files can be lengthy, the Comparison Tool provides toolbar buttons to help you step through the results from one difference to the next.

To navigate through comparison results:

Alternatively, use the menu items Comparison > Next or Previous.

Viewing a Summary of Differences

To see a summary of differences between two text files, scroll to the bottom of the Comparison Tool and view the list, which contains information such as:

Ignoring Whitespace Differences in Text Comparisons

You may want to hide whitespace differences to help you distinguish between functional changes and changes to indentation.

Use the toolbar button, , to toggle the display of differences only involving whitespace characters, or select Comparison > Ignore Whitespace.

Showing Differences Only

You can specify whether to show only differences or entire files. It can be useful to hide unmodified lines in large text comparison reports. When you are showing differences only and sections are hidden, the report displays messages like the following: 10 unmodified lines hidden.

Use the toolbar button, , to toggle the display of sections of the report that do not contain any differences, or select Comparison > Show Differences Only.

Changing the Display Width of a Text Comparison

To increase or decrease the line lengths of the text files in the comparison display, edit the number in the Columns visible field. Resize the window, if necessary.

For details on other comparison tool features, see Using Comparison Tool Features.

Saving HTML Report

Click Save As on the toolbar to save a copy of the comparison report as an HTML file. The tool creates a corresponding folder containing the style sheet and JavaScript files that control the report highlighting. To preserve the highlighting, keep the folder with the HTML file.

Comparing Files with Autosave Version or Version on Disk

From the Editor you can compare one open text file with another, or you can choose to compare the latest version of a file in the Editor to an autosave version or a saved version. For an example, follow these steps:

  1. Open one of the text files you want to compare in the Editor.

    To open the example file provided, lengthofline.m, run the following command in the Command Window:

    open(fullfile(matlabroot,'help','techdoc','matlab_env',...
    	'examples','lengthofline.m'))
    
  2. Select Tools > Compare Against > Choose (or Save and Compare Against if your file is modified).

    Navigate to the file you want to compare against, select the file, and click Open. To open the example file provided, select lengthofline2.m from the folder where you found lengthofline.m. Other options available are:

    • To compare the open file to the Editor's automatic copy (filename.asv) select Tools > Compare Against > Autosave Version. For more information, see Autosaving Files.

    • To compare an open file that has been changed, but not saved, to the saved version, select Tools > Compare Against Version on Disk

Comparing MAT-Files

You can use the Comparison Tool to compare two MAT-files. The tool presents the variables in the two files side by side, which enables you to:

The Comparison Tool report highlights changes in variables as follows.

Change SummaryHighlightingNotes
ModifiedPinkValues of the variable differ between the two files.
Click the View differences button Image of Comparison Tool View differences button  as described in the text preceding the image. to investigate. A new variable comparison report opens to display differences in individual array elements or differing fields of a structure. Double-click pink rows or cells to investigate further layers of differences.
AddedGreenVariable only exists in right file.
RemovedPurpleVariable only exists in right file.
IdenticalNoneVariable identical in both files.
Class changedPink (only in Class columns)Variable data class changed. Click the View differences button to investigate.

Click the Merge button Image of Comparison Tool Merge button  as described in the text preceding the image. in the Merge column to copy modified variables from one file to the other.

The following image shows the results when you compare two files, data1.mat and data2.mat.

Image of Comparison Tool window with highlighting as described in the text preceding the image.

Comparing Binary Files

You can use the Comparison Tool to compare two binary files such as DLL files or MEX-files. Also, you can select the Binary comparison type for any pair of files with a choice of comparison types.

Using Comparison Tool Features

You can use the Comparison Tool for the following tasks:

Selecting Files or Folders to Compare from the Comparison Tool

To compare two files or folders from the Comparison Tool, follow these steps:

  1. Select Desktop > Comparison Tool to open the tool.

    The Comparison Tool opens a window that is empty, except for the title bar, a menu bar, and a toolbar.

  2. Compare files or folders by clicking the New comparison button or by selecting File > New Comparison.

    The dialog box Select Files or Folders for Comparison appears.

  3. In the dialog box, select two files or folders to compare. Use the drop-down lists or the Browse buttons Image of Select a file button to locate and select the items that you want to compare.

    You also can drag and drop a file or folder from Windows Explorer to the left and right file and folder fields.

  4. Optionally, choose the comparison type you want to use. Either use the default Comparison type value, or if multiple comparison types are available, select a different one from the list. For example, for text files you could select text or binary comparison types.

  5. Click Compare.

Exchanging the Left and Right Sides of the Report

To move the file or folder on the left side to the right side and vice versa, select File > Swap Sides, or click the Swap sides button .

Refreshing the Report to Show Updated Files

After making changes to and saving the files in the Editor, update the results in the Comparison Tool by selecting File > Refresh or clicking the Refresh button .

Finding Text

To find a phrase in the current display, select Edit > Find, or click the Find text button . The resulting Find dialog box is the same as the one you use in the Command Window. For more information, see Find Text Currently Displayed in the Command Window.

Viewing New Comparisons

You can perform another file comparison by selecting File > New Comparison, or by clicking the toolbar button .

New comparisons open additional tabs in the Comparison Tool.

Viewing Previous Comparisons

You can see the results of previous comparisons in the current session by selecting that comparison's tab entry on the document bar at the bottom of the window. If you close the Comparison Tool, the current and previous comparisons are lost.

Function Alternative for Comparing Files and Folders

Use the visdiff function to open the Comparison Tool from the Command Window.

visdiff(fileorfoldername1, fileorfoldername2)

For example, type:

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


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS