Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Comparing Files and Folders

What Is 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:

Comparing Two Text Files

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:

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

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

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

Comparing Two MAT-Files

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:

To compare two MAT-files, follow these steps:

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

  2. Click the New file comparison button: .

  3. In left side of the File and Folder Comparisons window, click the Select a File button Image of Select A file button. Then browse to and select the name of one of the MAT-files that you want to compare.

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

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

  5. To view the contents of a variable in the Variable Editor, click the name of that variable.

  6. To load the variables for a specified file into the workspace, click a load link.

Comparing Two Binary Files

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.

Comparing Two Folders

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:

To compare two folders, follow these steps:

  1. Select Desktop > File and Folder Comparisons.

    The File and Folder Comparisons window opens.

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

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

    Image of File and Folder 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 folder names that are highlighted in dark red to refresh the File and Folder Comparisons tool with the two highlighted folders presented for comparison.

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

Using Features of the File and Folder Comparisons Tool

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

Navigating from One Difference to the Next

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:

Increasing or Decreasing 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 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.

Exchanging Positions

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 .

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

Finding 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 Finding Text Currently Displayed in the Command Window.

Viewing a Summary of Differences

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:

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

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

  1. Locate the file and folder field Image of the file and folder field. above the file that you want to replace.

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

Viewing 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 folder comparison by selecting File > New Folder Comparison or clicking the New folder comparison button .

Viewing 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 Folder Comparisons tool, the current and previous comparisons are lost.

Accessing the File and Folder Comparisons Tool

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:

Supply the files or folders to compare as described in Comparing Two Text Files and Comparing Two Folders, respectively.

Function Alternative for Comparing Files and Folders

Use the visdiff function to open the File and Folder Comparisons tool from the Command Window. For example, type:

visdiff('lengthofline.m', 'lengthofline2.m')
  


Recommended Products

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