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
visdifffunction. For example, to compare the two files
visdiff('data1.mat','data2.mat'). MATLAB opens the Comparison Tool and displays the resulting comparison report.
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,
data2.mat. When you compare
the two files, the Comparison Tool displays the resulting report.
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.
|Values of the variable differ between the two files. Click the compare link to investigate.
|The variable only exists in the right file.
|The variable only exists in the left file.
|The variables in both files are equivalent. The tool ignores
NaN patterns, field ordering in
structs, and the difference between negative zero or positive
|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.
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.
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.