How to Delete Scatter Plot Points From a Figure in Matlab

It is often useful to update the location of scatter plot points overlaid on a Matlab figure, especially when displaying the incremental results of iterative algorithms. In order to do this, the existing scatter plot points need to be deleted prior to redrawing new scatter points.

In order to delete points which have been plotted on an axis with scatter(), the following code can be used:

delete(findobj(gca, 'type', 'patch'));

It is not immediately obvious from the documentation, but scatter points are considered to be a “patch” type object. Thus, this line of code finds all patch objects on the current axes and deletes them.

To delete the 2D lines drawn by Matlab’s plot() function a similar statement can be used, in this case by changing the type to the more obvious value of “line”:

delete(findobj(gca, 'type', 'line'));

How to Create a Random UUID in Matlab

Generating a universally unique identifier (UUID) can be very useful for a variety of tasks where you need a unique way to identify something. While a UUID is not technically guaranteed to be unique, the identifier space is so large (128-bit) that it can be treated as unique for most purposes.

It is simple to generate a UUID string in MATLAB by using its built in Java Virtual Machine:

uuid = char(java.util.UUID.randomUUID);

The function java.util.UUID.randomUUID() will return an object of type java.util.UUID, but this can be typecasted into a MATLAB string through the char() function. Simple!

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:

I1 = zeros(500,'uint8');
I2 = zeros(500,'uint8')+127;
I3 = zeros(500,'uint8')+255;

subplot(1,3,1), imshow(I1);
subplot(1,3,2), imshow(I2);
subplot(1,3,3), imshow(I3);


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():

function h = subplottight(n,m,i)
    [c,r] = ind2sub([m n], i);
    ax = subplot('Position', [(c-1)/m, 1-(r)/n, 1/m, 1/n])
    if(nargout > 0)
      h = ax;

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

I1 = zeros(500,'uint8');
I2 = zeros(500,'uint8')+127;
I3 = zeros(500,'uint8')+255;

subplottight(1,3,1), imshow(I1, 'border', 'tight');
subplottight(1,3,2), imshow(I2, 'border', 'tight');
subplottight(1,3,3), imshow(I3, 'border', 'tight');


Solution: Colormap missing during MATLAB figure export to Illustrator

I usually export my MATLAB figures to Adobe Illustrator .AI format. However, I have noticed that if my figure contains a colorbar, the colormap is blank when I open the AI file in Illustrator.

To fix this, I found that you can instead save the figure as an EPS file (*.eps, standing for Encapsulated PostScript). This file can be opened in Illustrator, and still contains all the layer and path information I need.

Another useful trick I use before exporting a figure with colormaps is to increase the step size of the colormap. By default, the colormap contains 64 colors, but you can usually see the edge between each color. To smooth out the colormap, I like to use:


This tells the colormap to use 256 colors instead of only 64. Of course, the colormap gray can be replaced by any other colormap name, e.g., hsv or jet.