Documentation Center

  • Trial Software
  • Product Updates

visdiff

Compare two text files, MAT-Files, binary files, Zip files, or folders

Syntax

visdiff('fname1', 'fname2')
visdiff('filename1', 'filename2', 'type')

Description

visdiff('fname1', 'fname2') opens the Comparison Tool and presents the differences between the two files or folders. Either ensure that the two files or folders appear on the MATLAB® path, or provide the full path for each file or folder. You can compare files or any combination of folders, zip files, or Simulink® manifests.

If you have Simulink Report Generator™ software, you can select a pair of Simulink models to compare XML text files generated from them.

visdiff('filename1', 'filename2', 'type') opens the Comparison Tool and presents the differences between the two files using the specified comparison type. type can be 'text' or 'binary'. If you do not specify type, visdiff creates the default comparison type for your selected files. The type option does not apply when comparing folders.

If there are multiple comparison types available for your selections (e.g., text, binary, file list, or XML comparison), you can create a new comparison and choose a different comparison type.

  1. In the Comparison Tool, click the New comparison button.

    The dialog box Select Files or Folders for Comparison opens and shows your previous comparison selections in the drop-down lists.

  2. Change the comparison type and click Compare.

Examples

Specifying Files or Folders to Compare

The visdiff function accepts fully qualified file names, relative file names, or names of files on the MATLAB path.

If the files you want to compare appear on the MATLAB path or in the current folder, you can specify the file names without the full path, for example:

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

or

visdiff('lengthofline','lengthofline2')

If the files you want to compare are not on the path, either specify the full path to each file, or add the folders to the path.

For example, to specify the fully qualified file names to compare two example files:

visdiff(fullfile(matlabroot,'toolbox','matlab','demos','gatlin.mat'), ...
fullfile(matlabroot,'toolbox','matlab','demos','gatlin2.mat'))

Specify the full path to files as follows:

visdiff('C:\Work\comp\lengthofline.m', 'C:\Work\comp\lengthofline2.m')

You can specify paths to files relative to the current folder. For the preceding example, if the current folder is Work, then the relative paths are:

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

Compare Two Text Files

To view a comparison of the two example files, lengthofline.m and lengthofline2.m:

visdiff(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'), fullfile(matlabroot,'help',...
'techdoc','matlab_env','examples','lengthofline2.m'))

For information about using the report features, see Comparing Text Files.

    Note:   If the text files you compare are XML files, you see different results if you have MATLAB Report Generator installed. For details, see Comparing Files and Folders.

Compare Two MAT-Files

To compare two example files:

visdiff(fullfile(matlabroot,'toolbox','matlab','demos','gatlin.mat'), ...
fullfile(matlabroot,'toolbox','matlab','demos','gatlin2.mat'))

For information about the report features, see Comparing MAT-Files.

Compare Two Binary Files

The following example code adds a folder containing two MEX-files to the MATLAB path, and then compares the files:

addpath([matlabroot '\extern\examples\shrlib'])
visdiff('shrlibsample.mexw32', 'yprime.mexw32')

The Comparison Tool opens and indicates that the files are different, but does not provide details about the differences.

For more information on binary comparisons, see Comparing Binary Files.

Compare Two Folders or Zip Files

You can perform file list comparisons for any combinations of folders and zip files. To view an example folder comparison and instructions for using the report features, see Comparing Folders and Zip Files.

Compare Files and Specify Type

To compare two example text files and specify comparison type as binary:

visdiff(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'), fullfile(matlabroot,'help',...
'techdoc','matlab_env','examples','lengthofline2.m'), 'binary')

If you do not specify type, visdiff creates the default comparison type for your selected files, in this case, text comparison. By changing to the binary comparison type you could examine differences such as end-of-line characters.

Similarly, when you compare XML files without specifying type, you get a hierarchical XML comparison report. If instead you want a text or binary comparison, you can specify "text" or "binary" comparison types to see more details. When you compare zip files, the default comparison type is a file list comparison, and you might want to specify a binary comparison instead.

Alternatives

As an alternative to the visdiff function, compare files and folders using any of these GUI methods:

  • From the Current Folder browser:

    • Select a file or folder. Right-click the file or folder, and select Compare Against.

    • For two files or subfolders in the same folder, select the files or folders. Then, right-click, and select Compare Selected Files/Folders.

  • From the MATLAB desktop, on the Home tab, in the File section, click Compare and then select the files or folders to compare.

  • If you have a file open in the Editor, on the Editor tab, in the File section, click Compare. Alternatively, under Compare, you can choose a file to compare against, or compare with the autosave version or the version on disk. See Comparing Files with Autosave Version or Version on Disk.

Was this topic helpful?