| MATLAB® | ![]() |
| On this page… |
|---|
What Is the File and Directory Comparisons Tool? |
The File and Directory Comparisons tool determines and displays the differences between two files or two 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 directories to determine which file names are unique to each directory.
Compare two directories to determine if files with the same name in each directory have the same content.
When you use the File and Directories 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 directory 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, file name.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 Directory 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 side of the page but not in the file presented on the left side of the page.
Green highlighting and a < at the end of a line indicate a line that exists in the file presented on the left side of the page but not in the file presented on the right side of the page.

Use the features of the File Comparison and Directory 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.
When you use the File and Directory Comparisons tool to compare two MAT-files, a window opens and presents the variables in the two files side by side. The tool 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 Directory Comparisons.
The File and Directory Comparisons window opens.

Click the
New File Comparison button:
.
Drag one of the MAT-files that you want to compare from the Current Directory browser or Windows® Explorer to the left side of the File and Directory Comparisons window.
Drag one of the MAT-files that you want to compare from the Current Directory browser or Windows Explorer to the right side of the File and Directory Comparisons window.
The File and Directory 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 presented on the right, but not in the file presented on the left.
Purple highlighting indicates a variable that exists in the file presented on the left, but not in the file presented 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.

Click a variable name to view its contents in the Variable Editor.
Click a load link to load the specified file's variables into the workspace.
When you use the File and Directory 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 Directory Comparisons tool to compare two directories, a window opens and presents the contents of the directories, side by side. The tool enables you to:
Determine the files that the directories have in common.
Determine if files with identical names that are common to both directories also have identical content.
Open for comparison two files that are common to both directories, but have different content.
Open for comparison two subdirectories that are common to both directories, but have different content.
Open a file for viewing in the Editor.
To compare two directories, follow these steps:
Select Desktop > File and Directory Comparisons.
The File and Directory Comparisons window opens.
Select File > New Directory Comparison or click the New Directory Comparison
button
.
The File and Directory Comparisons window refreshes with a Type a directory name here field on each side of the tool.
Type or browse to a directory on each side of the tool.
The File and Directory Comparisons tool displays the contents of the directories side by side and highlights files and subdirectories that do not match, as follows:
Pink highlighting indicates that the contents of the files or subdirectories differ.
Green highlighting indicates a file or subdirectory that exists in the directory on the right, but not in the directory on the left.
Purple highlighting indicates a file or subdirectory that exists in the directory on the left, but not in the directory on the right.
The following image shows an example of the File and Directory Comparisons tool when two directories 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 directory names that are highlighted in pink to refresh the File and Directory Comparisons tool with the two highlighted directories presented for comparison.
Click the compare link next to a set of file names that are highlighted in pink to refresh the File and Directory Comparisons tool with the two highlighted files presented for comparison.
The File and Directory Comparisons tool provides features that let you do any of the tasks described in the following sections:
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 you want to keep the window size narrow, but that 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 directory 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 Find Dialog Box.
If the tool is currently comparing files, you can replace an existing file in the tool by doing one of the following:
Drag a different file name from the Current Directory browser or Windows Explorer to the left or right side of the File and Directory Comparisons tool, replacing the file currently shown there.
Type the path to a file or browse to find a file using the field below the File and Directory Comparisons toolbar.
Select File > Open.
If the tool is currently comparing directories, you can replace an existing directory by typing the path to a directory or browsing to a directory using the field below the File and Directory Comparisons toolbar.
You can perform
another file comparison by selecting File > New File Comparison or clicking
the New File Comparison button
.
You can perform another directory
comparison by selecting File > New Directory Comparison or
clicking the New Directory Comparison button
. Supply the files or
directories to compare as described in Replacing a File or Directory Being Compared with Another File
or Directory.
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 Directory 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 Directory Comparisons tool using one of these methods:
From the MATLAB® desktop, select Desktop > File and Directory Comparisons.
From the Current Directory browser, select a file or directory, right-click, and from the context menu, select Compare Against.
For two files or subdirectories in the same directory, from the Current Directory browser, select the files or directories, right-click, and from the context menu, select Compare Selected Files or Compare Selected Directories.
Supply the files or directories to compare as described in Replacing a File or Directory Being Compared with Another File or Directory.
![]() | Finding Text in Files | Keyboard Shortcuts in the Editor | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |