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:
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 or Live Editor tab, in the File section,
Click Compare to browse to a second file for comparison.
Alternatively, under Compare, select Compare with Version on Disk or Save and Compare with Autosave. See Comparing Files with Autosave Version or Version on Disk. These options are not available for live scripts.
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
If you specify two files or folders to compare using either
the Current Folder browser or the
then the Comparison tool automatically performs the default comparison
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.
Comparison 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 and Live Scripts.
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
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. For information, see Model Comparison.
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
open the File and Folder Filter dialog box and enter:
To ignore all files in a folder named
but not ignore subfolders, enter:
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.
|Difference Summary||Highlighting for Files and Folders||Notes|
|Contents changed||Pink||The contents of the files or folders differ. Click the compare link to investigate.|
|Added or Removed||Green||File or folder only exists in the left or right list.|
The following image shows an example of the Comparison tool when two folders are compared. The results are sorted by Type.
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.
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'))
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.
When you compare live scripts, the Comparison tool converts
all formatted content to publish markup. This generates the same code
as when you save a live script as a MATLAB Code file (
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
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
in each file matches, even though the
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.
endstatement because in performing this operation, it finds that the last line in one file does not match the last line in the other file.
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.
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. Merging is not available when comparing live scripts.
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.
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
show you that the file contains unsaved changes.
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.
To save your changes, click Save Merged File. To save to a different name, select Save Merged File > Save Merged File As.
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.
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
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.
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:
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.
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.
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.
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:
Open one of the text files you want to compare in the Editor.
To open the example file provided,
run the following command in the Command Window:
On the Editor or Live 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
lengthofline2.m from the folder where
Other options available are:
To compare the open file to the Editor's automatic
under Compare, 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 been changed, but not saved, to the saved version, under Compare, select Compare with Version on Disk.
Note: To select files to compare, see Select the Files or Folders to Compare.
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.
|Modified||Pink||Values 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.
|Added or Removed||Green||Variable only exists in left or right file.|
|Equivalent||None||The variables in both files are equivalent. The tool ignores differences in NaN patterns, field ordering in structs, and negative zero or positive zero.|
|Class changed||Pink (only in Class columns)||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 contain ignored differences.
In the Merge column, click the Merge button to copy modified variables from one file to the other.
The following image shows the results when you compare two files,
If values of the variable differ between the two files, you can click the compare link to investigate. A new variable comparison report opens. See Comparing Variables.
To view an example MAT-file comparison, enter:
visdiff(fullfile(matlabroot,'toolbox','matlab','demos','gatlin.mat'), ... fullfile(matlabroot,'toolbox','matlab','demos','gatlin2.mat'))
The variable comparison report displays 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 select files to compare, see Select the Files or Folders to Compare.
Note: If you are comparing SLX files, and do not have Simulink Report Generator, you see a binary comparison. For information on SLX comparison reports instead, see Model Comparison.
You can use the Comparison tool to compare two binary files
such as DLL files or MEX-files. Also, you can select the
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')
You can use the Comparison tool for the following tasks:
To compare two files or folders from the Comparison tool, follow these steps:
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.
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 first and second file and folder fields.
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.
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.
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.
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.
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 comparison reports.
You can change and save your diff color preferences for the Comparison tool. You can apply your color preferences to all comparison types.
On the MATLAB Home tab, click Preferences.
In the Preferences dialog box, under MATLAB, click Comparison.
Edit color settings as desired for differences, modified lines, modified contents, and merged lines. View the colors in the Sample pane.
The Active Settings list displays Default (modified).
To use your modified settings in the comparison, click Apply and refresh the comparison report.
To return to the default settings, in the Preferences dialog box, click Reset and click Apply. Refresh the comparison report.
If you want to save your 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 settings, you can select them in the Active Settings list.
to open the Comparison tool from the Command Window.
For example, type: