Matlab Logo One of the built in capabilities within MATLAB that I’ve been using a lot lately is the File Comparison Tool. This tool allows you to quickly compare two different files by breaking down the similarities and differences between the two files in a format that is simple and easy to understand. In this tutorial, you will learn how to open the tool and how to use it. Below is an image of the tool in action.

File Comparison GUI

Contents

Where is the File Comparison Tool located within MATLAB?

You can open up the File Comparison Tool by first going to the Desktop menu and then selecting File Comparisons as shown in the image below. Notice that I’m using version R2006. I believe it is the same for the versions after 2006, but I think it is located elsewhere for the earlier versions of MATLAB.

File Comparison GUI

How to Use the File Comparison Tool

  1. When you bring up the tool, you’ll get something that looks like this:
  2. File Comparison GUI

  3. You can either browse for your input files, or you can just drag and drop them. I prefer the second method personally.
  4. File Comparison GUI

  5. After you input the two files you want to compare, you’ll get something like this.
  6. File Comparison GUI

Interpreting the Results

In the image above, you’ll notice there are different colors of text.

  • Grey text denotes matching text between the two files.
  • Green background denotes content that is in one file, but not in the other.
  • Red background denotes content that share some similarities, but not a perfect match.

Also, if you look closely, you can also see the line numbers of each file, which can be helpful in keeping track of the data. Finally, at the end of the comparison (which is not shown in the image above), there is a tally of the number of matching lines.

What is the File Comparison Tool useful for?

The file comparison tool is pretty nifty as it quickly allows you to compare files to ascertain the similarities and differences. Here are two reasons why I use this tool:

  1. Comparing two versions of an m-file. Sometimes we update our m-files but we forget what the changes were. This will quickly tell you what code was deleted/added or modified! Ideally, you probably want to implement a versioning system that helps you keep track of m-files and the changes that were made. It’s a good practice to adopt and will save you time and heartache in the future.
  2. Comparing two data sets. Sometimes, we think two data sets are exactly the same when in fact they aren’t. Datasets can be very large, and if you’re like me, a cursory inspection of the first 5 lines of data to make sure they are matching is usually a comprehensive check to make sure that two files have the same contents.

Here’s some extra information on the file comparison tool over at Ken and Mike’s Blog that shows you how to use some of the more advanced features.

Can you guys think of other ways where this tool might prove useful? I only use it for the two reasons stated above, but I’m sure you guys can find more ingenious methods to use this tool!