Trinh @ Bath

Generating nice images in Matlab

Here are some brief details and tips for developing a good workflow for generating publication-quality images.

A workflow for saving images

As you become a more experienced researcher, you will learn the value of developing a workflow where images can be easily re-generated. This means that you can easily replot the images when you are finalising the presentation.

  1. Rule: Save all data or all scripts. Get in the habit of saving data as .mat files.
  2. Tip: use scripts to generate your figures. Create scripts that will load in .mat files and plot the data. This means if you ever need to regenerate pictures, you can do it very easily. If you are performing a numerical experiment, make sure to comment the filename or comment the file directly, indicating important clues about when the figure was generated, and the conditions.
  3. Tip: save your figures as .fig. Similar to above, you may want to save .fig files of the pictures. Again, it allows fine-tuning. (In fact, you can extract raw data from .fig files via scripts if you look into it). To save such a file, simply click File > Save-As on the figure window.

Exporting figures

Traditionally (meaning literally in the last 20 years), Matlab has had very poor ability to generate publication-quality images. Problems include: poor bounding boxes, weird rasterisation effects, weird layering issues, etc.

Always, always, always whenever possible, export your images in PDF. This is currently the best format for including into LaTex. When done in the vectorised format of a PDF, the image is then scalable.

There are exceptions to the above rule in the context of images that are extremely data-hungry (thousands of points) or involve 3D surfaces, contour plots, etc. In those cases, the export to PDF can cause file sizes that are too large, or can cause weird layering effects (due to the complexity of rendering a surface, say). In those cases, you want to export to a rasterised format like PNG.

Use export_fig package (Matlab file exchange) to export to pdf format.

Once you have downloaded the export_fig package, unzip it and move the folder into a unified functions folder on your computer. Refer to the startup.m guide. By doing so, you can issue a command like

export_fig 'test.pdf' 

in order to produce a PDF of the currently focused figure.

Annotations

In order to add annotations to figures, you have a few options:

  1. Add the annotation natively within Matlab using commands like text(…); this is a good idea in combination with scripts.
  2. Add the annotation afterwards by using a vector editor like https://en.wikipedia.org/wiki/Inkscape. This is not a bad idea and can be quite powerful. However note that it can be difficult due to LaTeX fonts not importing nicely into Inkscape.
  3. Use a native LaTeX package like tikz or pdfpages

Advanced

More 'advanced' figure generation can involve things like TikZ, which generates figures natively within LaTeX. The advantage of this approach is consistency in annotation.

Post-2010 there have been very powerful scripts that allow Matlab figures to be automatically converted to TiKZ, such as matlab2tikz.

If you are an undergrad student or under pressure to write a report, I don't recommend trying to learn this!