Have you ever found a new feature in an application that you use daily and then wondered, “How did I not notice that before?”While using MATLAB a few weeks back, I discovered a feature that I had never noticed before that has been a part of MATLAB for a full year now; since MATLAB 7.3 (R2006b), MATLAB has had a diff tool built into it.
Some might be wondering, “What is a diff tool?” A diff tool allows you to compare two files against each other so that you can see where the differences between them are. I find that using a diff tool can be very helpful just before I checkin code after adding a feature or fixing a bug. By looking at the differences between my file and the original version of the file I can very easily review all of my changes to the file. This helps ensure that the code that I am checking in is correct.
You might be saying, “So how do I use the diff tool from within MATLAB?” It is actually very easy to get to. I recently decided to update one of my contributions on the MATLAB Central – File Exchange and when I was done I wanted to use the diff tool to see what I had changed. In the Current Directory window, if you select two files (by holding in the Control key while selecting the files) and then right click on the files, you will see the context menu shown below:
After clicking, “Compare Selected Files,” a new window is opened with the changes highlighted:
Diff tools are not perfect, and sometimes they show changed lines as new lines, but the view that they provide can really help you know where things have changed within your code.I was really happy when I found this feature was built into MATLAB. Let me know once you give this tool a try, and if like me, you find this very useful.
Comments are closed.
9 CommentsOldest to Newest
This color diff in MATLAB is a really useful for comparing two individual files. I like the ability to click on a line that differs and be taken right to the editor with that line highlighted. Thanks!
Unix / Linux has a wonderful option to diff that I use a lot: “diff -r”, which recursively compares two directories. It’s a very useful for looking at two versions of a project with hundreds of files. MATLAB users could really benefit from it, too, I think.
Consider this usage scenario: a user selects two directories (instead of two files) and then selects the menu item which now is worded “Compare Selected Directories”. Then a file-by-file diff appears, comparing each file that matches between the two directories (and subdirectories), or “Only in …” for files that appear in only one directory or another. You would normally skip binary files – just do *.m, *.c, and so forth (perhaps selecting filetypes with another option).
While viewing a single file-diff, it would be nice to have an option of not displaying the greyed out text (lines that are identical between the two files), or to just display a few grey lines for context.
Thanks for the feedback. The ability to compare entire directories is something you’ll likely see in some future release.
We’ll take a look at ways to better handle large runs of identical content.
Readers might be interested to know that the useful diff tool can be ran programatically, as described recently in http://www.mathworks.com/matlabcentral/newsreader/view_thread/161225
I hope it doesn’t break in future versions (it very well might since it uses unsupported functionality), but in the meantime it saves the need to interactively specify the input files. If possible, I would love Matlab to have a supported function that does this (and similar functions for all interactive functionality).
Great point Yair! There actually is an undocumented function visdiff which will do part of what your asking for, namely comparing two files. It only takes file names as input right now.
I’ve created an enhancement request to make this function more robust..
As always, note that this is an undocumented function, so it’s availability and signature may change in the future.
Thanks for the information. I am a beginner and this helped me great deal to figure out a stupid mistake of mine. Thanks a lot.
You are welcome. I am happy to see that this article is still helpful nearly 3 years after I wrote it!
Thank you so much; I had been looking for the diff of Matlab for quite some time!
This was really helpful. Thanks for posting this three years ago!
Wish I had come across this article in 2007 already, not 2013…