How to Make a Borderless Subplot of Images in MATLAB

Let’s say that you have a set of images that you want to tile using imshow() and subplot() in a MATLAB figure. By default, both functions add a padded space around the images to separate them, as this example shows:

Result:

However, what if you want to tile the images without any space between them? The imshow() function does have a property to remove the border around a displayed image, by using imshow(I, 'border', 'tight'). This is fine when only one image is being displayed, but subplot() itself adds additional spacing between images. Removing this space is not straightforward, but a gap-less subplot grid can be constructed by using the following function in place of subplot():

By using this function, a completely borderless subplot of images can be constructed as follows:

Result: