You can customize external source control tools to use the MATLAB® Comparison Tool for diff and merge. If you want to compare MATLAB files such as live scripts, MAT, SLX, or MDL files from your source control tool, then you can configure your source control tool to open the MATLAB Comparison Tool. The MATLAB Comparison Tool provides tools for merging MathWorks® files and is compatible with popular software configuration management and version control systems. You can use the automerge tool with Git™ to automatically merge branches that contain changes in different subsystems in the same SLX file.
To set up your source control tool to use MATLAB as the application for diff and merge, you must first determine the full paths
of the mlDiff
, mlMerge
, and
mlAutoMerge
executable files, and then follow the recommended steps
for the source control tool you are using.
To get the required file paths and enable external source control tools to reuse open MATLAB sessions, run this command in MATLAB:
comparisons.ExternalSCMLink.setup()
This command sets the MATLAB preference, under Comparison, called Allow external source control tools to use open MATLAB sessions for diffs and merges.
This command also displays the file paths to copy and paste into your source control tool setup:
On Windows®:
Diff: matlabroot\bin\win64\mlDiff.exe
Merge: matlabroot\bin\win64\mlMerge.exe
AutoMerge: matlabroot\bin\win64\mlAutoMerge.exe
On Linux®:
Diff: matlabroot/bin/glnxa64/mlDiff
Merge: matlabroot/bin/glnxa64/mlMerge
AutoMerge: matlabroot/bin/glnxa64/mlAutoMerge
On Mac:
Diff: matlabroot/bin/maci64/mlDiff
Merge: matlabroot/bin/maci64/mlMerge
AutoMerge: matlabroot/bin/maci64/mlAutoMerge
where matlabroot
is replaced with the full
path to your installation, for example, C:\Program
Files\MATLAB\R2020b
.
Note
Your diff and merge operations use open MATLAB sessions when available, and only open MATLAB when necessary. The operations only use the specified MATLAB installation.
To configure MATLAB diff and merge tools with command-line Git:
Run this command in MATLAB.
comparisons.ExternalSCMLink.setupGitConfig()
This command displays the full paths of the mlDiff
,
mlMerge
, and mlAutoMerge
executable files. It also automatically populates the global
.gitconfig
file. For
example:
[difftool "mlDiff"] cmd = \"C:/Program Files/MATLAB/R2020b/bin/win64/mlDiff.exe\" $LOCAL $PWD/$REMOTE [mergetool "mlMerge"] cmd = \"C:/Program Files/MATLAB/R2020b/bin/win64/mlMerge.exe\" $PWD/$BASE $PWD/$LOCAL $PWD/$REMOTE $PWD/$MERGED [merge "mlAutoMerge"] driver = \"C:/Program Files/MATLAB/R2020b/bin/win64/mlAutoMerge.exe\" %O %A %B %A
Note
You need to do step 1 only once for your Git setup.
Configure your repository to use the mlAutoMerge
executable file. Open the .gitattributes
file in your
repository and
add:
*.slx binary merge=mlAutoMerge
Now, when you merge branches that contain changes in different subsystems in the same SLX file, MATLAB handles the merge automatically.
To run the MATLAB diff and merge tools from command-line Git, use git difftool
and git
mergetool
:
To compare two revisions of a model using the MATLAB diff tool, type:
git difftool -t mlDiff <revisonID1> <revisionID2> myModel.slx
If you do not provide revision IDs, git difftool
compares the working copy to the repository copy.
If you do not specify which model you want to compare, command-line Git will go through all modified files and ask you if you want to compare them one by one.
To resolve a merge conflict in a model using the MATLAB merge tool, type:
git mergetool -t mlMerge myModel.slx
If you do not specify which model you want to merge, command-line Git will go through all files and ask you if you want to merge them one by one.
SourceTree is an interactive GUI tool that visualizes and manages Git repositories for Windows and Mac.
Configure the MATLAB diff and merge tools as SourceTree external tools:
With SourceTree open, click Tools > Options.
On the Diff tab, under External Diff / Merge, fill the fields with the following information:
External Diff tool:Custom
Diff Command: C:\Program Files\MATLAB\R2020b\bin\win64\mlDiff.exe Arguments: $LOCAL $PWD/$REMOTE Merge tool:Custom
Merge Command: C:\Program Files\MATLAB\R2020b\bin\win64\mlMerge.exe Arguments: $PWD/$BASE $PWD/$LOCAL $PWD/$REMOTE $PWD/$MERGED
Configure your repository to automerge changes in different subsystems in
the same SLX file using the mlAutoMerge
executable file:
Open the global .gitconfig
file and
add:
[merge "mlAutoMerge"] driver = \"C:/Program Files/MATLAB/R2020b/bin/win64/mlAutoMerge.exe\" %O %A %B %A
Open the .gitattributes
file in your
repository and
add:
*.slx binary merge=mlAutoMerge
Tip
Customize the full path of the mlDiff
,
mlMerge
, and mlAutoMerge
executables
to match both the MATLAB installation and the operating system you are using. For more
information, see Finding the Full Paths for MATLAB Diff, Merge, and AutoMerge.
To use the MATLAB diff tool from within SourceTree, right-click a modified file under Unstaged files and select External Diff.
To use the MATLAB merge tool when SourceTree detects a merge conflict, select the Uncommitted changes branch, right-click a modified file, and select Resolve Conflicts > Launch External Merge Tool.
With TortoiseSVN, you can customize your diff and merge tools based on the file extension. For example, to use MATLAB diff and merge tools for SLX files:
Right-click in any file explorer window and select TortoiseSVN > Settings to open TortoiseSVN settings.
In the Settings sidebar, select Diff Viewer. Click Advanced to specify the diff application based on file extensions.
Click Add and fill the fields with the
extension and the mlDiff
executable path:
Filename, extension or mime-type: .slx External Program: "C:\Program Files\MATLAB\R2020b\bin\win64\mlDiff.exe" %base %mine
Click OK and repeat the same steps to add another file extension.
In the Settings sidebar, select Diff ViewerMerge Tool. Click Advanced to specify the merge application based on file extensions.
Click Add and fill the fields with the
extension and mlMerge
executable path:
Filename, extension or mime-type: .slx External Program: "C:\Program Files\MATLAB\R2020b\bin\win64\mlMerge.exe" %base %mine %theirs %merged
Click OK and repeat the same steps to add another file extension.
You can now use the MATLAB tools for diff and merge the same way you would use the TortoiseSVN default diff and merge applications.
Note
Automerging binary files with SVN , such as SLX files, is not supported.
With Perforce® P4V, you can customize your diff and merge tools based on the file extension. To use MATLAB diff and merge tools for SLX files, for example:
In Perforce, click Edit > Preferences.
In the Preferences sidebar, select Diff. Under Specify diff application by extension (overrides default), click Add.
In the Add File Type dialog box, enter the following information:
Extension: .slx
Application: C:\Program Files\MATLAB\R2020b\bin\win64\mlDiff.exe
Arguments: %1 %2
Click Save.
In the Preferences sidebar, select Merge. Under Specify merge application by extension (overrides default), click Add.
In the Add File Type dialog box, enter the following information:
Extension: .slx
Application: C:\Program Files\MATLAB\R2020b\bin\win64\mlMerge.exe
Arguments: %b %2 %1 %r
Click Save and repeat the steps for other file extensions.
Tip
Customize the full path of the mlDiff
and
mlMerge
executables to match both the MATLAB installation and the operating system you are using. For more
information, see Finding the Full Paths for MATLAB Diff, Merge, and AutoMerge.
You can now use the MATLAB tools for diff and merge the same way you would use the Perforce default diff and merge applications.