Main Content

Compare and Merge Live Scripts and Functions

You can compare two live code files and merge changes between one file and another using the Comparison Tool. The Comparison Tool highlights differences in code, text, and text formatting.

Select Files to Compare

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

  • Live Editor — Go to the Live Editor tab and, in the File section, click Compare. The Comparison Tool includes the currently open file in the First file or folder field. Click the open 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.

  • Current Folder browser — Select a file, right-click, and select Compare Against. Click the open 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 averageweight.mlx and averageweight2.mlx using the visdiff function and the default text comparison, type visdiff('averageweight.mlx','averageweight2.mlx'). MATLAB® opens the Comparison Tool and displays the resulting comparison report.

Explore Differences

When you perform a live code comparison, a new window opens and displays the two files side by side. For example, suppose that you have two files, averageweight.mlx and averageweight2.mlx, with several differences. When you compare the two files, the Comparison Tool displays the resulting report.

When comparing live code files, the Comparison Tool highlights differences in code, text, and text formatting. The Comparison Tool ignores output, even if the file contains saved output. If the tool detects a difference within an equation, image, or control, the entire item is highlighted. The Comparison Tool displays the total number of differences in the bottom-right corner of the comparison report, and highlights the lines that do not match using the colors listed in this table.

Highlight ColorDescription
PurpleThe line contains differences.
Dark PurpleThe highlighted characters are different.
BlueThe line exists only in right file. This can happen when the line is inserted in the right file, or when it is deleted from the left file. The corresponding line in the left file is highlighted using a gray striped pattern.
YellowThe line exists only in left file. This can happen when the line is inserted in the left file, or when it is deleted from the right file. The corresponding line in the right file is highlighted using a gray striped pattern.

The Comparison Tool attempts to match lines and detects code and text that is added, deleted, or changed. For example, in the live code comparison of averageweight.mlx and averageweight2.mlx, the tool determines that averageweight.mlx has two lines of code that do not exist in averageweight2.mlx and highlights them (line 5 and line 7) in yellow. Also, the tool takes the additional lines into account and determines that the line containing the disp statement in each file matches, even though the disp statement does not occur on the same line number.

To step through the results one difference at a time, use the Next and Previous buttons.

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

Merge Changes

When comparing live code files, you can merge changes from one file to the other. Merging changes can be useful when resolving conflicts between different versions of files. You only can merge from left to right. If you want to merge into the left file, click the Swap Sides button before you start merging. Clicking the Swap Sides button reverts any merges already made and creates a new comparison report for the original files.

To begin merging, on the Comparison tab, click the Merge Mode button. Then, to replace content from the right pane with content from the left pane, click the button located next to the line you want to merge. You also can select a difference and, in the Merge tab, click the Replace Content button.

The right pane contains the merged result. An asterisk next to the merged file name in the right pane (filename_modified.mlx*) indicates that the file contains unsaved changes.

To undo a replacement, click the button between the modified lines or on the Comparison tab. To revert all merges and start again, click the Refresh button. You also can click the Refresh button to update the comparison report after making changes to and saving the files in the Live Editor. Refreshing discards all unsaved merged changes.

To save your changes and return to the comparison report, click the Save Result button. To return to the comparison report without saving changes, click the Return to Comparison button.

See Also

Related Topics