Main Content

Compare and Merge MAT-Files

You can use the Comparison Tool to compare two MAT-files. Compare two MAT-files to:

  • Determine which variables are common to both files and which appear in only one file.

  • Identify differences between individual variables. For example, you can determine which fields of a structure are different or which elements of an array differ.

Select Files to Compare

There are multiple ways to select two files and start the Comparison Tool:

  • MATLAB® desktop — Go to the Home tab and, in the File section, click Compare. Click the Browse to select File or Folder button to select items to compare, or drag and drop files from your file browser into the First file or folder or Second file or folder fields.

  • Current Folder browser — Select a file, right-click, and select Compare Against > Choose.... Click the Browse to select File or Folder button to select a second item to compare, or drag and drop a file from your file browser into the Second file or folder field. To select two files or subfolders to compare, Ctrl-click the file names. Then right-click and select Compare Selected Files/Folders.

  • Command Window — Use the visdiff function. For example, to compare the two files data1.mat and data2.mat using the visdiff function, type visdiff('data1.mat','data2.mat'). MATLAB opens the Comparison Tool and displays the resulting comparison report.

Explore Differences

When you perform a MAT-file comparison, a new window opens and displays the two files side by side. For example, suppose that you have two MAT-files, data1.mat and data2.mat. When you compare the two files, the Comparison Tool displays the resulting report.

Comparison Tool comparing data1.mat to data2.mat

The Comparison Tool highlights changes in variables. If the variables in both files are equivalent but the files are not identical, the Comparison Tool displays a message. Possible causes of the differences between the files include file formats, file timestamps, the order in which the variables are stored, or variables that contain ignored differences. The Comparison Tool uses these colors to highlight differences.

PurpleValues of the variable differ between the two files. Click the compare link to investigate.
BlueThe variable only exists in the right file.
YellowThe variable only exists in the left file.
NoneThe variables in both files are equivalent. The tool ignores differences in NaN patterns, field ordering in structs, and the difference between negative zero or positive zero.
Purple (only in Class columns)The variable data class changed. Click the compare link to investigate.

You can further explore the results in the report using several methods:

  • Sort results — To sort the results by name, size, class, or difference summary, click the corresponding column header. For example, click the Class column header to sort by class type.

  • Load variable — To load the contents of a variable into the Variable Editor, click the name of that variable.

  • Load MAT-file — To load one of the MAT-files into the workspace, click the corresponding Load link at the bottom of the report.

  • Save HTML report — To save a copy of the report as an HTML file, go to the Comparison tab and click Save As > HTML.

For information about how to further configure the comparison report, see Compare Files and Folders and Merge Files.

Compare Variables

To investigate differences between instances of a variable in both files, click the compare link in the Difference Summary column of the comparison report table. The Comparison Tool displays a report for the specified variable. To further investigate differences in individual array elements or fields of a structure, double-click a highlighted row.

Comparison Tool comparing variables in two files, with channels variable highlighted

Merge Changes

When comparing MAT-files, you can merge changes from one file to the other. There are no undo capabilities when merging MAT-files, so merge with caution.

To copy modified variables from one file to the other, on the Comparison tab, in the Merge section, click either the Copy the right variable to the left file or the Copy the left variable to the right file button, as applicable.


In MATLAB Online™, the File Comparison Tool does not support variable comparison.

See Also

Related Topics