| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
What Is the File and Folder Comparisons Tool? Using Features of the File and Folder Comparisons Tool |
The File and Folder Comparisons tool determines and displays the differences between two files or two folders (sometimes referred to as directories).
You can use this tool to:
Compare lines in two text files (some other applications refer to this as a file diff operation).
Compare variables in two MAT-files.
Determine whether the contents of two binary files are the same.
Compare two folders to determine which file names are unique to each folder.
Compare two folders to determine if files with the same name in each folder have the same content.
When you use the File and Folders Comparisons tool to compare two text files, a window opens and presents the two files side by side, along with symbols to indicate how you can adjust the files to make them match. This is useful, for example, when you want to compare the latest version of a text file to an autosave version.
To compare two text files, follow these steps:
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'))
Select Tools > Compare Against > Browse. 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 the following:
Tools > Compare Against > Autosave Version to compare the open file to the Editor's automatic copy, filename.asv. For more information, see Autosave.
Tools > Compare Against Version on Disk to compare an open file that has been changed, but not saved, to the saved version.
The File and Folder Comparisons tool opens, displaying the files side by side and highlighting lines that do not match, as follows:
Pink highlighting and an x at the start of a line indicate that the content of the lines differs between the two files.
Green highlighting and a > at the start of a line indicate a line that exists in the file presented on the right, but not in the file presented on the left.
Green highlighting and a < at the end of a line indicate a line that exists in the file presented on the left, but not in the file presented on the right.

Use the features of the File and Folder Comparison tool to work with the results.
Typically, when this tool compares two text files, it does not do a simple line-by-line comparison. In the previous image, for example, the tool determines that lengthofline2.m, has a line of code that does not exist in lengthofline.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 being compared are extremely long, however, the tool may run out of memory in attempting to perform the file comparison. It then displays the message, Maximum file length exceeded. Defaulting to line-by-line comparison. In this case, the tool highlights the lines containing the end statement because in performing a simple line-by-line comparison it finds that the last line in one file does not match the last line in the other file.
You can use the File and Folders Comparisons tool to compare two MAT-files. The tool presents the variables in the two files side by side, which enables you to:
See which variables are common to each file and which are unique.
Load the contents of the variables into the Variable Editor.
Load the MAT-files into the workspace.
To compare two MAT-files, follow these steps:
Select Desktop > File and Folder Comparisons.
The File and Folder Comparisons window opens a dialog box that is empty except for the title bar, menu bar, and a toolbar.
Click the New file comparison button:
.
In left side of the File and Folder Comparisons window, click the Select
a File button
. Then browse to and select
the name of one of the MAT-files that you want to compare.
In right side of the File and Folder Comparisons window, enter the full file name of the other MAT-file that you want to compare.
The File and Folder Comparisons tool displays the file variable names side by side and highlights variables that do not match, as follows:
Pink highlighting indicates that the values of the variables differ between the two files.
Green highlighting indicates a variable that exists in the file displaying on the right, but not in the file displaying on the left.
Purple highlighting indicates a variable that exists in the file displaying on the left, but not in the file displaying on the right.
The following image shows the results when you compare matlabroot/toolbox/matlab/demos/gatlin2.mat to matlabroot/toolbox/matlab/demos/gatlin.mat. To determine your matlabroot folder, type matlabroot in the Command Window.

To view the contents of a variable in the Variable Editor, click the name of that variable.
To load the variables for a specified file into the workspace, click a load link.
When you use the File and Folder Comparisons tool to compare two non-MAT-file binary files, such as DLL files or MEX-files, the tool returns a message indicating whether the files are the same.
To compare two binary files, follow the same steps in Comparing Two MAT-Files. 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. MATLAB cannot display the differences between files of these types.
When you use the File and Folder Comparisons tool to compare two folders, a window opens and presents the contents of the folders, side by side. The tool enables you to:
Determine the files that the folders have in common.
Determine if files with identical names that are common to both folders also have identical content.
Open for comparison two files that are common to both folders, but have different content.
Open for comparison two subfolders that are common to both folders, but have different content.
Open a file for viewing in the Editor.
To compare two folders, follow these steps:
Select Desktop > File and Folder Comparisons.
The File and Folder Comparisons window opens.
Select File > New Folder Comparison or click the New folder comparison button
.
The File and Folder Comparisons window refreshes with a Type a folder name here field on each side of the tool.
Type or browse to a folder on each side of the tool.
The File and Folder Comparisons tool displays the contents of the folders side by side and highlights files and subfolders that do not match, as follows:
Light red highlighting indicates that the contents of the files differ.
Dark red highlighting indicates that the contents of the subfolders differ
Light green highlighting indicates a file that exists in the folder on the right, but not in the folder on the left.
Dark green highlighting indicates a subfolder that exists in the folder on the right, but not in the folder on the left.
Light blue highlighting indicates a file that exists in the folder on the left, but not in the folder on the right.
Dark blue highlighting indicates a subfolder that exists in the folder on the left, but not in the folder on the right.
The following image shows an example of the File and Folder Comparisons tool when two folders are compared.

Click the open link next to a file name to open that file in the Editor.
Click the compare link next to a set of folder names that are highlighted in dark red to refresh the File and Folder Comparisons tool with the two highlighted folders presented for comparison.
Click the compare link next to a set of file names that are highlighted in light red to refresh the File and Folder Comparisons tool with the two highlighted files presented for comparison.
The File and Folder Comparisons tool provides features that let you do any of the tasks described in the following sections:
Because text files can be lengthy, the File and Folder Comparisons tool provides links to help you navigate from one difference to the next. Do one of the following to display a different set differences, relative to the current set of differences displaying in the Editor:
To navigate to a previous set of lines that differ, click the up arrow.
If there is no previous set of lines that differ, the up arrow takes you to the beginning of the file.
To navigate to the next set of lines that differ, click the down arrow.
If there are no additional sets of lines that differ, the down arrow takes you to the end of the file.

When comparing text files, the display is 60 columns wide, by default. To increase the display width, type a high number in the Columns visible field, and then drag the vertical edges of the window to make it wider. If keeping the window size narrow results in more columns appearing for the file on the left than for the file on the right, reduce the number for Columns visible to display a sufficient number of columns for both files, given the window width.
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
.
After making changes to and saving the files in the Editor,
update the results in the File Comparisons tool by selecting File > Refresh or clicking the Refresh button
.
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 Finding Text Currently Displayed in the Command Window.
To see a summary of difference between two text files, scroll to the bottom of the File and Folder Comparisons tool. There you find a list such as the following:
Number of matching lines:52
Number of unmatched lines in left-hand file: 12
Number of unmatched lines in right-hand file: 15
If the tool is currently comparing files, replace an existing file in the tool by doing the following:
Locate the file and folder field
above the file that you
want to replace.
Do one of the following:
Drag a file from Windows Explorer to the file and folder field.
Type the path to a file in the file and folder field.
Click the Browse for file button
next to the file and folder field, and then
select a file.
Place the cursor in the file and folder field, and then select File > Open.
If the tool is currently comparing folders, you can replace an existing folder. Type the path to a folder, or browse to a folder in the file and folder field.
You can perform another file comparison by selecting File > New File Comparison or clicking the New file comparison button
.
You can perform another folder comparison by
selecting File > New Folder Comparison or clicking the New folder comparison button
.
You can see the results of previous comparisons in the current session by selecting that comparison's entry in the document bar (as shown at the bottom of the window in the illustration in Comparing Two Text Files). If you close the File and Folder Comparisons tool, the current and previous comparisons are lost.
In addition to the methods shown in the previous sections, you can also access the File and Folder Comparisons tool using one of these methods:
From the MATLAB desktop, select Desktop > File and Folder Comparisons.
From the Current Folder browser, select a file or folder, right-click, and from the context menu, select Compare Against.
For two files or subfolders in the same folder, from the Current Folder browser, select the files or folders, right-click, and from the context menu, select Compare Selected Files or Compare Selected Folders.
Supply the files or folders to compare as described in Comparing Two Text Files and Comparing Two Folders, respectively.
Use the visdiff function to open the File and Folder Comparisons tool from the Command Window. For example, type:
visdiff('lengthofline.m', 'lengthofline2.m')
![]() | Finding Text in Files | Saving, Printing, and Closing Files in the Editor | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |