Making MATLAB figures 2 - gcf and print

The appearance of a new MATLAB figure never fails to disappoint.

Sadly, a raw MATLAB figure would never reach these dizzying heights.
Sadly, a raw MATLAB figure would never reach these dizzying heights.

Fortunately, we can adjust totally reinvent the figure using a few choice commands, which I will go through over the course of a few posts.

Get current figure

We’ll begin today with gcf, which stands for ‘get current figure’. Once you have created a figure (ideally, as described here), you can use gcf to modify its appearance.

There are a great many (60ish) different settings that can be controlled using gcf. To see them all - and their current settings - first create a figure, then type gcf into your Command Window, and click on all properties:

Create a figure, type gcf, and click 'all properties' to see all of your options.
Create a figure, type gcf, and click 'all properties' to see all of your options.

We don’t care about most of them, so I’ll just highlight the ones that I use regularly here.

Using gcf for the background colour

There are two ways to adjust each property; which one you use is entirely a matter of personal preference. The first uses the set function and MATLAB’s name-value pairs. The second is object-oriented, which may come more naturally if you are familiar with other coding languages. Here, we change the figure’s background colour to white using both approaches:

% MATLAB-y approach: set(gcf, 'name', value)
set(gcf, 'color', 'w')

% Object-oriented approach: gcf.Name = value;
gcf.Color = 'w';

The main difference is that in the first approach, you can pile in as many name-value pairs as you like into a single set function, whereas the second approach requires a new line for each setting. Also, the setting ‘name’ in the first approach is not case-sensitive, whereas it is in the second.

Figure size and shape

As well as background colour, some other gcf settings that we will most often want to change are PaperUnits, Units, PaperSize, and PaperPosition. These settings allow us to direct the size that the figure will be when saved using the print function. PaperUnits and Units set the units (e.g. cm) for the other two inputs. PaperSize sets the size of the figure in those units. PaperPosition sets the position of the drawing within the file (i.e. controls the margins).

Avoid undesired colour changes

Another handy setting is InvertHardCopy. By default this is set 'on' which can sometimes cause the colours of a saved figure to be different from how you’ve set them. Change it to 'off' to avoid this.

Put it in a function

I advise you to forget almost all of these details immediately. It’s far too long-winded to type out these settings over and over again for every figure, especially when they will look so similar each time, with just the actual size values changing. So, let’s make a function to do it all for us!

function printsetup(cf, size_cm)

    set(cf, 'Color', 'w',  'InvertHardCopy', 'off')
    set(cf, 'PaperUnits', 'centimeters',  'Units', 'centimeters')
    set(cf, 'PaperSize', size_cm)
    set(cf, 'PaperPosition', [0 0 size_cm])

end % function printsetup

This sets the background colour to white, and prevents unwanted colour changes. Then it sets the figure size to be the input size_cm, and removes margins. The gcf for the current figure should be given as the input cf. (You could also rewrite this using the object-oriented approach.)

Technical aside: when using set, the settings are not applied until after set has been run. So if one setting depends upon another, they need to be in separate set functions, in the correct order. In the example above, PaperSize and PaperPosition must come separately from and after PaperUnits and Units; if they were all within the same set function, then the default units would be used, instead of centimeters.

Save the above function (as printsetup.m - MATLAB function files must have exactly the same name as the function) somewhere that’s permanently on your MATLAB path. Now, whenever you make a new figure, you can just use this function with whatever size you want the final figure to be. For example, for a 9 by 7 cm figure:

figure(1); clf
printsetup(gcf, [9 7])

See here for more on the opening line.

The size isn’t right?

The figure window that pops up won’t appear to have the dimensions that you have set. The sizes that you’ve set apply to the figure that is generated by the print function:

figure(1); clf
printsetup(gcf, [9 7])

% Code to plot draw the figure goes here

print('-r300', 'fig_filename', '-dpng')

This saves a 9 by 7 cm figure as fig_filename.png with a resolution of 300 dpi. Hopefully you can see where each of those properties is set in the code; have a look into the docs on the print function (type it in MATLAB and hit F1).

Workflow with print

This brings us to a key point about the workflow of figure creation, which arises because the output from print does not look exactly the same as in the MATLAB figure window. The most efficient workflow is therefore:

  1. Set up the bulk of the figure by looking at the MATLAB figure window, without using print, and without worrying about positioning and fine detail;
  2. Add the print command;
  3. Adjust positioning and fine detail looking at the saved file, not the MATLAB figure window.

By ‘bulk of the figure’ in step 1, I mean put all the data on there, set the colours, axis limits, ticks, labels, and so on. By ‘positioning and fine detail’ in 3, I mean the exact sizes and location(s) of the axes, and precise marker sizes and line widths. More discussion on both of these parts will follow in later posts.

In summary

  • gcf allows to you control the overall appearance of a figure;
  • Set up a function (like the printsetup example above) to apply your commonly-used settings in one go;
  • gcf and print used together can produce precisely-sized raster figures with whatever resolution you need;
  • Make most of the figure just looking at the MATLAB figure window, then add print and fine-tune the appearance from the saved file.