Compare Files and Folders and Merge Files
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.
Select the Files or Folders to Compare
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
visdifffunction. For example, to compare the two files
visdifffunction and the default text comparison, type
visdiff('lengthofline.m','lengthofline2.m'). MATLAB opens the Comparison Tool and displays the resulting comparison report.
When selecting folders to compare, select Include subfolders to include subfolder content in the comparison.
Choose a Comparison Type
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.
Explore the Comparison Report
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.
Compare Folders and Zip Files
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.
Compare Text Files
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.
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 in the bottom-right corner of the comparison report and highlights the lines that have changed with the colors listed in this table.
|Purple||The line contains differences.|
|Dark Purple||The highlighted characters are different.|
|Blue||The line exists only in the right file. This can happen when the line is inserted in the right file, or when it is deleted from the left file. The corresponding line in the left file is highlighted using a gray striped pattern.|
|Yellow||The line exists only in the left file. This can happen when the line is inserted in the left file, or when it is deleted from the right file. The corresponding line in the right file is highlighted using a gray striped pattern.|
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:
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, click Filter, and select Ignore White Space.
Show compared files details — To show the file details, click the expand arrow next to the file name.
Save HTML Report — To save a copy of the comparison report as an HTML file, click Publish > 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 Next and Previous buttons.
By default, XML files are compared using a text comparison. To compare XML files with a hierarchical comparison instead, see Compare XML Files.
Merge Text 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 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 Comparison 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 (
lengthofline2.m *) indicates that the file contains
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. Refreshing discards all unsaved 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 Binary Files
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
Compare Other File Types
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).
Additional Comparison Tools
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.
External Source Control Integration Preferences
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 sessions, in the Comparison preferences External Source Control Integration section, select the Allow 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).