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:
Select the files or folders to compare.
Choose a comparison type.
Explore the comparison report.
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 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 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 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 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
visdiff function and the default text
MATLAB opens the Comparison Tool and displays the resulting comparison
When selecting folders to compare, select Include subfolders to include subfolder content in the comparison.
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.
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.
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.
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,
curvefittings2, with several
differences. When you compare the two folders, the Comparison Tool displays the
The Comparison Tool highlights files and subfolders that do not match using the colors listed in this table.
|Purple||The contents of the files or folders differ. Click the compare link to investigate.|
|Blue||The file or folder only exists in the right list.|
|Yellow||The file or folder only exists in the left list.|
|None||The 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.
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 > "
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/. To ignore all files in a folder named
CVS, but not ignore subfolders, type
To edit existing filters, double click the filter.
For information about how to further configure the comparison report, see Additional Comparison Tools.
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.
Symbols in between the two 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
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 total number of differences at the top of the comparison report and highlights the lines that have changed with the colors listed in this table.
|Purple||The line contains differences. The Comparison Tool displays an x in the column between the two files.|
|Dark Purple||The highlighted characters are different.|
|Blue||Line exists only in right file. The Comparison Tool displays a > in the column between the two files.|
|Yellow||Line exists only in left file. The Comparison Tool displays a < in the column between the two files.|
The Comparison Tool attempts to match lines and detects text that is added, deleted,
or changed. For example, in the text comparison of
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, 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
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
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.
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 and buttons. 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.
If the files you are comparing are extremely long, the Comparison Tool can 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.
lengthofline2.mfor example, the tool highlights the lines containing the
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.
By default, XML files are compared using a text comparison. To compare XML files with a hierarchical comparison instead, see Compare XML 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 merge a change 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
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. Before making any changes in the Editor, save your merge changes from the comparison report. Otherwise, the comparison report can become incorrect. The report does not update to reflect the changes you make in the Editor.
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
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).
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.
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.
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.
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 session, in the Comparison preferences External Source Control Integration section, select theAllow 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).
If you encounter errors like
java.lang.OutOfMemoryError: Java heap
java.lang.OutOfMemoryError: GC Overhead limit
exceeded when comparing large files, then edit your MATLAB preferences to increase the heap size.
On the Home tab, in the Environment section, click Preferences.
Select MATLAB > General > Java Heap Memory.
Move the slider to increase the heap size, and then click OK.