Documentation Center

  • Trials
  • Product Updates

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:

  • From the Current Folder browser:

    • Select a file or folder, right-click and select Compare Against, and browse to select a second item to compare.

    • For two files or subfolders in the same folder, select the files or folders, right-click and select Compare Selected Files/Folders.

  • If you have a file open in the Editor, on the Editor tab, in the File section,

  • From the MATLAB® desktop, on the Home tab, in the File section, click Compare. Select the files or folders to compare.

  • From the command line, use the visdiff function.

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.

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 Select 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:

  • Compare and merge lines in two text files (some other applications refer to this as a file diff operation). See Comparing Text Files.

  • Compare and merge variables in two MAT-files. See Comparing MAT-Files.

  • Determine whether the contents of two binary files match. See Comparing Binary Files.

  • Compare any combination of folders, zip files, or Simulink® manifests to determine:

    • Which file and folder names are unique to each list

    • If files and folders with the same name in each list have the same content

    See Comparing Folders and Zip Files.

  • Compare XML files:

    • If you select XML files to compare and you have MATLAB Report Generator™ software, the Comparison Tool runs a hierarchical matching algorithm. You then see a report showing a hierarchical view of the portions of the two XML files that differ.

    • If you have Simulink Report Generator software, you can select a pair of Simulink models to compare XML files generated from them.

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:

  • Determine the files that the comparison lists have in common.

  • Determine if files with identical names that are common to both comparison lists also have identical content.

  • Open a new comparison of two files or folders that are common to both comparison lists, but have different content.

  • Open a file for viewing in the Editor.

  • Specify filters to ignore certain files or folders

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, on the View tab, click the Filter button, , then select Add/Remove Filter.

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

Next Steps Using the Report

To explore the report you can use the following tools:

  • You can sort the results by name, type, size, or last modified timestamp by clicking the column headers. For example, click the Type column header to sort by folder and file type, as shown in the preceding figure.

  • To open a new comparison of two files or folders with changed contents, click the compare link next to file or folder names highlighted in pink.

  • 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 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, as shown in the next figure. You can click the links to Skip Current item or Cancel All to stop further analysis.

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

Comparing Text Files

Select Text Files to Compare

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

To view an example text comparison, enter:

visdiff(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'), fullfile(matlabroot,'help',...
'techdoc','matlab_env','examples','lengthofline2.m'))

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:

  • Dark pink highlighting indicates changed characters within lines.

  • Pink highlighting and an x between the two files indicate that the content of the lines differs between the two files.

  • Green highlighting and a right (>) or left (< ) angle bracket between the two files indicate a line that exists on one side only.

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 24) 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.

Step Through Differences

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

To navigate through comparison results:

  • Click the right arrow button to go to the next set of lines that differ.

    If no additional sets of lines differ, the right arrow takes you to the end of the file.

  • Click the left arrow button to go to a previous set of lines that differ.

    If no previous set of lines differ, the left arrow takes you to the beginning of the file.

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

  1. Select a difference in the report and click the Merge button to copy the selected difference from the left file to the right file.

    Merged differences display gray row highlighting, and a green merge arrow.

    The merged file name at the top of the report displays the dirty flag (filename.m*) to show you that the file contains unsaved changes.

  2. To revert the last merge operation, click Undo in the Merge section. You can click Undo repeatedly, or Redo to reapply a merge.

      Tip   You can click Swap Sides to start again and revert all merges.

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

  4. If you want to inspect the files in the Editor, click the line number links 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.

View 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:

  • Number of matching lines: 51

  • Number of unmatched lines in left-hand file: 13

  • Number of unmatched lines in right-hand file: 16

Ignore White-Space Differences in Text Comparisons

You may want to hide white-space differences to help you distinguish between functional changes and changes to indentation.

On the View tab, click the Filter button, and check or uncheck the Ignore White Space item to toggle the display of differences only involving white-space characters.

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

On the View tab, click the Filter button, and check or uncheck the Show Differences Only item to toggle the display of sections of the report that do not contain any differences.

Change the Display Width of a Text Comparison

You can increase or decrease the line lengths of the text files in the comparison display. On the View tab, in the Display section, edit the number in the Column Width edit box. Resize the window, if necessary.

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

Save HTML Report

On the Comparison tab, in the Comparison section, click Save As > Save as HTML to save a copy of the comparison report as an 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. On the Editor tab, in the File section, click Compare. If your file is modified, the Editor saves the file before comparing. Alternatively, under Compare, select Save and Compare with.

    Navigate to the file you want to compare against, select the file, and click Open. For example, select the example file 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), under Compare, select Save and Compare with Autosave. If your file is modified, the Editor saves the file before comparing. For more information, see Autosaving Files.

    • To compare an open file that has been changed, but not saved, to the saved version, under Compare, select Compare with 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:

  • View and sort by the name, size, class, and change summary of all variables.

  • View details of differences between variables, to see which fields of a structure are different, and view differences in individual elements of an array.

  • Merge changes between files by copying modified variables from one file to the other (Caution: No undo).

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

  • Load the contents of the variables into the Variable Editor by clicking the name of that variable.

  • Load the MAT-files into the workspace by clicking a Load link.

  • Save a copy of the report as an HTML file. Click Save As > Save as HTML on the toolstrip.

The Comparison Tool report highlights changes in variables as follows.

Change SummaryHighlightingNotes
ModifiedPinkValues of the variable differ between the two files.
Click the compare link 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 left 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 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.

If values of the variable differ between the two files, you can click the compare link 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, as shown in the following example.

To view an example MAT-file comparison, enter:

visdiff(fullfile(matlabroot,'toolbox','matlab','demos','gatlin.mat'), ...
fullfile(matlabroot,'toolbox','matlab','demos','gatlin2.mat'))

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.

  • If the files are the same, the tool displays the message: The files are identical.

  • If the files differ, the tool displays the message: The files are different.

If the files differ, you can click the Show Details link to view the binary files and the byte offset of the first difference.

To view an example binary comparison, compare two example text files and specify comparison type as binary:

visdiff(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'), fullfile(matlabroot,'help',...
'techdoc','matlab_env','examples','lengthofline2.m'), 'binary')

Using Comparison Tool Features

You can use the Comparison Tool for the following tasks:

Select Files or Folders to Compare from the Comparison Tool

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

  1. From the MATLAB desktop, on the Home tab, in the File section, click Compare. Select the files or folders to compare.

    If the Comparison Tool is already open, compare files or folders by clicking the New button.

    The dialog box Select Files or Folders for Comparison appears.

  2. In the dialog box, select two files or folders to compare. Use the drop-down lists to select recent comparison items, or the Browse buttons 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.

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

  4. Click Compare.

Exchange 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, on the Comparison tab, in the Comparison section, click the Swap Sides button.

Refresh the Report to Show Updated Files

After making changes to and saving the files in the Editor, to update the results in the Comparison Tool, on the Comparison tab, in the Comparison section, click the Refresh button.

Find Text

To find a phrase in the current display, on the Comparison tab, in the Navigate section, click the Find button.

The resulting Find dialog box is the same as the one you use in the Command Window. For more information, see Find Text in Command Window or History.

Create New Comparisons

To perform another file or folder comparison, on the Comparison tab, in the Comparison section, click the New button.

The dialog box Select Files or Folders for Comparison appears, with the last comparison files preselected in the first and second file fields. Use the drop-down lists to select recent comparison items, or the Browse buttons to locate and select the items that you want to compare.

New comparisons open additional tabs in the Comparison Tool. You can drag tabs out of the tool if you want a separate window.

View 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')
Was this topic helpful?