visdiff
R2026bCompare two files or folders
Description
visdiff(
opens the Comparison Tool and displays the differences between the two files or
folders. The Comparison Tool supports MATLAB® code files, ZIP files, MAT files, and various other MATLAB, Simulink®, and scientific data file formats. For a complete list, see Input Arguments.filename1,filename2)
Use visdiff to compare two Simulink models, identify changes, and merge differences. For more information,
see Model Comparison (Simulink).
To close all currently opened Comparison
Tools, at the MATLAB Command Window, run comparisons.closeAll. (since R2026b)
compares two files and returns a comparison object that contains the differences
between the specified files. Use the comparison object to manipulate the comparison
at the command line, for example by applying filters and publishing comparison
reports, or to examine the comparison result directly at
the MATLAB Command Window (since R2026b). This syntax does not open the Comparison
Tool and does not require a display. It is particularly useful for automating the
creation of comparison reports for continuous integration (CI) workflows. This
syntax does not support all file types. Supported files include Simulink models, Simulink data dictionaries, requirement and link set files, plain text files,
MATLAB scripts, MATLAB apps, MAT files, and text-based source code files.comparison = visdiff(___)
Examples
This example shows how to compare two files using relative and full paths.
Compare the two files lengthofline.m and lengthofline2.m in the current folder.
visdiff("lengthofline.m","lengthofline2.m")
Compare the two MAT files gatlin.mat and gatlin2.mat using a fully qualified file name.
visdiff(fullfile(pwd,"supportingFiles","mymatFiles","gatlin.mat"), ... fullfile(pwd,"supportingFiles","mymatFiles","gatlin2.mat"))
Compare the two files lengthofline.m and lengthofline2.m as binary.
If you do not specify the comparison type, visdiff compares the two files using the default text comparison type. By changing to the binary comparison type you can examine differences such as end-of-line characters.
visdiff("lengthofline.m","lengthofline2.m","binary")
When comparing certain file types, you can manipulate the comparison report at the command line by specifying an output argument. Supported files include Simulink models, Simulink data dictionaries, requirement and link set files, plain text files, MATLAB scripts, MATLAB apps, MAT files, and text-based source code files.
For example, compare two Simulink model files and return a comparison object.
comparison = visdiff(modelname1,modelname2);
You can disable all filters from the model comparison report.
filter(comparison,"unfiltered");To publish a comparison report to a file, use publish on the comparison object. The default format of the published report is HTML. publish saves the file in the current folder as filename1_filename2.html.
file = publish(comparison); web(file)
Create a PDF comparison report named myreport. Save the report to the comparisonresults folder.
file = publish(comparison,format="PDF",Name="myreport",OutputFolder="comparisonresults"); web(file)
Supported report formats are HTML, PDF, PDF/A, and DOCX.
Note
PDF/A comparison reports are not supported on Linux®.
For instructions on how you can use visdiff to generate reports in your continuous integration workflows, see Attach Model Comparison Report to GitHub Pull Requests (Simulink).
Since R2026b
This example shows you how to compare two text files and examine the result of the comparison at the MATLAB Command Window.
Compare two files lengthofline.m and
lengthofline2.m and retrieve the comparison report
result from the comparison object
comp.
comp = visdiff("lengthofline.m","lengthofline2.m"); result = comp.Result
result =
TextDiffResult with properties:
Matches: [6x5 table]
HasChanges: 1The comparison report result is a
TextDiffResult object with these two properties:
Matches— Line-by-line comparison results, returned as a table.Column Type Description LeftLineNumberdoubleLine number in the left file. This value is NaNfor inserted lines.LeftLinestringText content from the left file. This value is missing for inserted lines. RightLineNumberdoubleLine number in the right file. This value is NaNfor deleted lines.RightLinestringText content from the right file. This value is missing for deleted lines. ChangeTypestringType of change. This value is equal to:
"unchanged"— The item is identical on both sides."modified"— The item exists on both sides but is different."inserted"— The item exists on the right side only."deleted"— The item exists on the left side only.
HasChanges— Whether the two compared files are different, returned as alogicalscalar.
Examine the line-by-line comparison results at the MATLAB Command Window.
result.Matches
LeftLineNumber LeftLine RightLineNumber RightLine ChangeType
______________ _____________________ _______________ _______________________ ___________
1 "function y = foo(x)" 1 "function y = foo(x)" "unchanged"
2 "% A simple function" 2 "% A modified function" "modified"
NaN 3 "y = x + 2;" "inserted"
3 "y = x + 1;" 4 "z = x * 3;" "modified"
4 "end" 5 "end" "unchanged"
5 "" 6 "" "unchanged"
Count the number of inserted lines.
numLinesInserted = sum(result.Matches.ChangeType == "inserted")
numLinesInserted =
1Count the number of deleted lines.
numLinesDeleted = sum(result.Matches.ChangeType == "deleted")
numLinesDeleted =
0Count the number of modified lines.
numLinesModified = sum(result.Matches.ChangeType == "modified")
numLinesModified =
2Since R2026b
This example shows you how to compare two folders and examine the result of the comparison at the MATLAB Command Window.
Compare two folders folder1 and
folder2 and retrieve the comparison report
result from the comparison object
comp.
comp = visdiff("folder1","folder2"); result = comp.Result
result =
TreeDiffResult with properties:
Matches: [5x4 table]
HasChanges: 1The comparison report result is a
TreeDiffResult object with these two properties:
Matches— Line-by-line comparison results, returned as a table.Column Type Description LeftPathcellPath segments of the left item, as displayed in the Comparison Tool, returned as a cell of string array. This value is missing for inserted items. RightPathcellPath segments of the right item, as displayed in the Comparison Tool, returned as a cell of string array. This value is missing for deleted items. ChangeTypestringType of change. This value is equal to:
"unchanged"— The item is identical on both sides."modified"— The item exists on both sides but is different."inserted"— The item exists on the right side only."deleted"— The item exists on the left side only.
DetailsstructParameter-level differences for a single matched item. The field of this structure is
ParameterDiffResultsand containsParameterDiffResultsobjects.This table shows the properties of the
ParameterDiffResultsobject.Column Type Description LeftNamestringParameter name in the left item. LeftValuestringParameter value in the left item. RightNamestringParameter name in the right item. RightValuestringParameter value in the right item. ChangeTypeType of change. This value is equal to "unchanged","modified","inserted", or"deleted".HasChanges— Whether the two compared files are different, returned as alogicalscalar.
Examine the line-by-line comparison results at the MATLAB Command Window.
result.Matches
LeftPath RightPath ChangeType Details
_________________________ _________________________ ___________ __________
{["sub" ]} {["sub" ]} "unchanged" 1x1 struct
{["sub" "nested.txt"]} {["sub" "nested.txt"]} "modified" 1x1 struct
{[ ]} {["added.txt" ]} "inserted" 1x1 struct
{["changed.txt" ]} {["changed.txt" ]} "modified" 1x1 struct
{["deleted.txt" ]} {[ ]} "deleted" 1x1 structCount the number of inserted items.
numItemsInserted = sum(result.Matches.ChangeType == "inserted")
numItemsInserted =
1Count the number of deleted items.
numItemsDeleted = sum(result.Matches.ChangeType == "deleted")
numItemsDeleted =
1Count the number of modified items.
numItemsModified = sum(result.Matches.ChangeType == "modified")
numItemsModified =
2Since R2026b
This example shows you how to compare two MAT files and examine the result of the comparison at the MATLAB Command Window.
Compare two MAT files mat1.mat and
mat2.mat and retrieve the comparison report
result from the comparison object
comp. In this example, the mat1.mat
file consists of a structure named x with a =
1, b = 2, and c = 3
fields. The mat2.mat file consists of a structure named
x with a = 1, b =
99, c = 3, and d = 4
fields.
comp = visdiff("mat1.mat","mat2.mat"); result = comp.Result
result =
TreeDiffResult with properties:
Matches: [3×4 table]
HasChanges: 1The comparison report result is a
TreeDiffResult object with these two properties:
Matches— Line-by-line comparison results, returned as a table.Column Type Description LeftPathcellPath segments of the left item, as displayed in the Comparison Tool, returned as a cell of string array. This value is missing for inserted items. RightPathcellPath segments of the right item, as displayed in the Comparison Tool, returned as a cell of string array. This value is missing for deleted items. ChangeTypestringType of change. This value is equal to:
"unchanged"— The item is identical on both sides."modified"— The item exists on both sides but is different."inserted"— The item exists on the right side only."deleted"— The item exists on the left side only.
DetailsstructParameter-level or text-level differences for a single matched item. The fields of this structure are
ParameterDiffResultsand/orTextDiffResults. Each field containsParameterDiffResultsandTextDiffResultsobjects respectively.This table shows the properties of the
ParameterDiffResultsobject.Column Type Description LeftNamestringParameter name in the left item. LeftValuestringParameter value in the left item. RightNamestringParameter name in the right item. RightValuestringParameter value in the right item. ChangeTypeType of change. This value is equal to "unchanged","modified","inserted", or"deleted".HasChanges— Whether the two compared files are different, returned as alogicalscalar.
Examine the line-by-line comparison results at the MATLAB Command Window.
result.Matches
LeftPath RightPath ChangeType Details
__________________________________________ __________________________________________ __________ __________
{["x (1x1 struct)" ]} {["x (1x1 struct)" ]} "modified" 1x1 struct
{["x (1x1 struct)" "b (1x1 double)"]} {["x (1x1 struct)" "b (1x1 double)"]} "modified" 1x1 struct
{[ ]} {["x (1x1 struct)" "d (1x1 double)"]} "inserted" 1x1 structCheck the parameter-level differences for the first item in the
Matches table. The report shows that the field names
of the x structure in the two MAT files are different,
because the x structure in the
mat2.mat file contains an additional
d
field.
result.Matches.Details(1).ParameterDiffResults.Matches
1×5 table
LeftName LeftValue RightName RightValue ChangeType
_____________ _________ _____________ ____________ __________
"Field Names" "a, b, c" "Field Names" "a, b, c, d" "modified"Count the number of top-level inserted variables. The x
structure in the mat2.mat file has an additional
d field.
matches = result.Matches;
isTopLevelVar = @isscalar;
numInsertedVars = sum(matches.ChangeType == "inserted" & cellfun(isTopLevelVar, matches.RightPath))numInsertedVars =
1Count the number of top-level deleted variables.
numDeletedVars = sum(matches.ChangeType == "deleted" & cellfun(isTopLevelVar, matches.LeftPath))
numDeletedVars =
0Count the number of top-level modified variables.
numModifiedVars = sum(matches.ChangeType == "modified" & cellfun(isTopLevelVar, matches.LeftPath))numModifiedVars =
1Since R2026b
This example shows you how to compare two Simulink models and examine the result of the comparison at the MATLAB Command Window.
Compare two Simulink models model_v1.slx and
model_v2.slx and retrieve the comparison report
result from the comparison object
comp.
comp = visdiff("model_v1.slx","model_v2.slx"); result = comp.Result
result =
ModelDiffResult with properties:
FilterSummary: "Applied filters: Nonfunctional, Ports, Annotations"
Matches: [7x6 table]
HasChanges: 1The comparison report result is a
ModelDiffResult object with these three properties:
FilterSummary— Summary of the active filters at the time of the comparison. To change the active filters after you already performed a comparison, use thefilterfunction of the comparison objectcompand retrieve the comparison reportresultagain.For example, to remove all currently active filters, run these commands:
filter(comp, 'unfiltered'); result_unfiltered = comp.ResultMatches— Model element comparison results, returned as a table.Column Type Description LeftPathcellPath segments of the left item, as displayed in the Comparison Tool, returned as a cell of string array. This value is missing for inserted items. RightPathcellPath segments of the right item, as displayed in the Comparison Tool, returned as a cell of string array. This value is missing for deleted items. ChangeTypestringType of change. This value is equal to:
"unchanged"— The item is identical on both sides."modified"— The item exists on both sides but is different."inserted"— The item exists on the right side only."deleted"— The item exists on the left side only.
DetailsstructParameter-level or text-level differences for a single matched item. The fields of this structure are
ParameterDiffResultsand/orTextDiffResults. Each field containsParameterDiffResultsandTextDiffResultsobjects respectively.This table shows the properties of the
ParameterDiffResultsobject.Column Type Description LeftNamestringParameter name in the left item. LeftValuestringParameter value in the left item. RightNamestringParameter name in the right item. RightValuestringParameter value in the right item. ChangeTypeType of change. This value is equal to "unchanged","modified","inserted", or"deleted".LeftObjectcellDomain-specific object:
Simulink.comparisons.SimulinkObject— Elements that a Simulink handle can represent, including blocks, lines, ports, and annotations.Stateflow® objects
missing— No object representation is available.
RightObjectcellHasChanges— Whether the two compared files are different, returned as alogicalscalar.
Examine the model element comparison results at the MATLAB Command Window. In this example, this command examines a single modified block.
result.Matches(3,:)
LeftPath RightPath ChangeType Details LeftObject RightObject
____________________ ____________________ __________ __________ ________________________________ ________________________________
{["Simulink","Const1"]} {["Simulink","Const1"]} "modified" 1x1 struct {1x1 SimulinkObject (Constant …)} {1x1 SimulinkObject (Constant …)}
The report states that a Constant block has been modified.
Check how the Constant block has been modified.
result.Matches.Details(3).ParameterDiffResults.Matches
LeftName LeftValue RightName RightValue ChangeType
________ _________ _________ __________ __________
"Value" "5" "Value" "10" "modified"The report states that the value of the Constant block changed from
5 to 10.
Count the number of deleted blocks.
matches = result.Matches; isBlock = @(obj) isprop(obj, "Handle") && get_param(obj.Handle, "Type") == "block"; numDeletedBlocks = sum(matches.ChangeType == "deleted" & cellfun(isBlock, matches.LeftObject))
numDeletedBlocks =
0Count the total number of modified parameters across all matched elements.
paramDiffResults = [matches.Details.ParameterDiffResults];
numModifiedParams = sum(vertcat(paramDiffResults.Matches).ChangeType == "modified")
numModifiedParams =
2If the model supports Stateflow operations, you can, for example, check the number of different states. Count the number of inserted states.
isState = @(obj) isa(obj, "Stateflow.State") numInsertedStates = sum(matches.ChangeType == "inserted" & cellfun(isState, matches.RightObject))
numInsertedStates =
1Input Arguments
File or folder name, specified as a character vector or string.
filename can include a relative path to the current
folder or a full path.
This table shows the supported file types.
| File Type | File Extension | |
|---|---|---|
| MATLAB | ||
| MATLAB code file | .m | |
| MATLAB live code file | .mlx, .m (since R2025a) | |
| Plain text | Any | |
| Text-based source code | .c, .cpp,
.java, etc. | |
| MATLAB App | .mlapp | |
| Binary | Any | |
| Folder | - | |
| ZIP file | .zip | |
| MATLAB figure | .fig | |
| MAT file | .mat | |
| Project definition files | .xml | |
| Project archive | .mlproj | |
| Dependency GraphML | .graphml | |
| Simulink | ||
| Simulink models | .slx, .mdl | |
| Simulink model template | .sltx | |
| Simulink project template | .sltx | |
| Simulink data dictionary | .sldd | |
| Other Products | ||
| Requirements Toolbox™ | .slreqx. For more information, see
Create and Save Printable Reports of Comparison Results (Requirements Toolbox). | |
| Requirements Toolbox links | .slmx. For more information, see Create and Save Printable Reports of Comparison Results (Requirements Toolbox). | |
| Simulink Test™ | .mldatx | |
| SimBiology® models | .sbproj | |
| System Composer® models | .slx | |
| Simscape® files | .ssc | |
| Scientific Data (since R2026a) | ||
| HDF5 file | .h5, .hdf5 | |
| NetCDF file | .nc | |
| SOFA file | .sofa | |
You can also use the Comparison Tool for basic comparisons for many other
file extensions including .prj, .req,
.cvf, .wrl,
.x3s, .ssc,
.xml, and .tlc.
Note
For HDF5, netCDF, and SOFA files, the Comparison Tool displays the differences in the schemas of the files. The schema comparison includes the hierarchical structure of the data as well as attributes and metadata, but it does not include the actual data stored in the files.
You can use the Comparison Tool to compare HDF5 files, netCDF-4 files, SOFA files, and Version 7.3 MAT files to each other. When you compare a Version 7.3 MAT file to an HDF5, netCDF-4, or SOFA file, only the schemas of the files are compared. (since R2026a)
Comparison type, specified as "text" or
"binary". Some comparisons do not support all of the
comparison types. If you specify a comparison type that is not supported,
MATLAB displays an error.
To examine differences such as end-of-line characters in text files,
specify the "binary" comparison type.
Output Arguments
Comparison object for manipulating the comparison at the command line, for example, by applying filters and publishing comparison reports, or for examining the comparison report at the MATLAB Command Window. Supported files include Simulink models, plain text files, MATLAB scripts, MATLAB apps, and text-based source code files.
Alternative Functionality
You can open the Comparison Tool interactively.
MATLAB editors — Open a file in the MATLAB Editor or in the Live Editor. In the Editor or Live Editor toolstrip, select Compare > Compare To. Then, select the file you want to compare to.
You can also compare a file with the autosave version or the saved version on disk. In the Editor tab, select Compare > Compare to Version on Disk or Compare to Backup. If your file is modified, the Editor saves the file before comparing. The Compare to Version on Disk and Compare to Backup options are only available if the option for automatically saving changes to a file is disabled. To disable the option, go to the Home tab, and in the Environment section, click Settings. Then, select Editor/Debugger and clear Save changes upon clicking away from a file. This option is not available in the Live Editor.
Simulink Editor — To compare a model that is open in the Simulink Editor to another model on disk, on the Modeling tab, in the Evaluate & Manage section, click Compare To.
Files or Project panel — To compare two files on disk, select both files. Then, right-click and select Compare Selected Files/Folders. The Comparison Tool opens the file you select first on the left.
Version History
Introduced in R2008bThe comparison object now has a new Result property. This
property contains the report of the comparison, such as line-by-line comparison. Use
this property to examine the comparison results directly at the MATLAB Command Window.
You can compare the schemas of HDF5, netCDF, and SOFA files. The Comparison Tool compares the schemas, but not the data, for these file types.
You can save comparison reports as a PDF/A file. PDF/A comparison reports are not supported on Linux.
You can return a comparison object when you compare Simulink data dictionaries or MAT files. Use the comparison object to publish comparison reports programmatically.
The XML comparison type has been removed. Overriding the default comparison type by
specifying "xml" is no longer supported. In R2025a, scripts that
use visdiff(filename1,filename2,"xml") issue an error.
Starting in R2023b, you can return a comparison object when you compare MATLAB apps. Use the comparison object to publish comparison reports programmatically.
Starting in R2023a, you can return a comparison object when you compare plain text files, MATLAB scripts, and text-based source code files. Use the comparison object to publish comparison reports programmatically.
The XML comparison type will be removed in a future release. Overriding the
default comparison type by specifying "xml" will not be possible
in a future release. In R2023a, scripts that use
visdiff(filename1,filename2,"xml") continue to work.
Starting in R2023a, you can return a comparison object when you compare
requirement (.slreqx) and link (.slmx) set
files. Use the comparison object to publish comparison reports
programmatically.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)