Comparing Files and Directories

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:

Comparing Two Text Files

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:

  1. 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'))
    
  2. 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:

    The File and Directory Comparisons tool opens, displaying the files side by side and highlighting lines that do not match, as follows:

    Image of File Comparisons dialog box with highlighting as described in the text preceding the image.

  3. 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.

Comparing Two MAT-Files

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:

To compare two MAT-files, follow these steps:

  1. Select Desktop > File and Directory Comparisons.

    The File and Directory Comparisons window opens.

    Image of File and Directory Comparisons window with no files open in it.

  2. Click the New File Comparison button: .

  3. 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.

  4. 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:

    The following image shows the results when you compare matlabroot/toolbox/matlab/demos/gatlin2.mat to matlabroot/toolbox/matlab/demos/gatlin.mat.

    Image of File and Directory Comparisons window with highlighting as described in the text preceding the image.

  5. Click a variable name to view its contents in the Variable Editor.

  6. Click a load link to load the specified file's variables into the workspace.

Comparing Two Binary Files

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.

Comparing Two Directories

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:

To compare two directories, follow these steps:

  1. Select Desktop > File and Directory Comparisons.

    The File and Directory Comparisons window opens.

  2. 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.

  3. 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:

    The following image shows an example of the File and Directory Comparisons tool when two directories are compared.

    Image of File and Directory Comparisons window with highlighting as described in the text preceding the image.

  4. Click the open link next to a file name to open that file in the Editor.

  5. 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.

  6. 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.

Using Features of the File and Directory Comparisons Tool

The File and Directory Comparisons tool provides features that let you do any of the tasks described in the following sections:

Increase or Decrease Line Lengths Shown for Text Files

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.

Exchange Positions

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 .

Show Updated Files

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 .

Find Text

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.

Replacing a File or Directory Being Compared with Another File or Directory

If the tool is currently comparing files, you can replace an existing file in the tool by doing one of the following:

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.

View New Comparisons

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.

View Previous Comparisons

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.

Alternative Ways to Access the Tool

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:

Supply the files or directories to compare as described in Replacing a File or Directory Being Compared with Another File or Directory.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS