Leader

Monday 31 August 2015

Matlab plot formats and how to quickly save high quality figures

Download
hgx.m
See also New graphics in Matlab 2014b and 2015 and creating nicer figures in one line of code for ng.m and og.m

The new graphics system in Matlab 2014b makes creating publication quality graphs much easier, but it still requires a little effort. See ng.m and og.m for two handy functions to improve the appearance of plots within Matlab. However, another important consideration is how to get the figure out of Matlab without ruining it. This function, hgx, is a handler for Matlab's various save/export functions and is designed to facilitate saving in useful forms using as little typing as possible




Generally, there are 3 kinds of format that Matlab can export to and that have different advantages depending on what the figure will be used for.

  • Raster formats (.jpg, .png, .tiff, etc.). Like a painting in real life, these formats are basically a fixed map of what colour each individual pixel should be. They differ in how they're compressed, but will always require some kind of interpolation to resize - which will lead to loss of quality, especially when scaling up. Also, some of these formats look horrible using the default export settings.
  • Vector formats (.svg etc.). These formats contain the data required to redraw a figure at any size. For example, a line would be saved as the equation required to redraw the line, rather than a fixed set of pixels. Handy, but requires extra processing power and logic to redraw, which can cause compatibility issues.
  • Matlab format (.fig). This is the Matlab format that contains the data to redraw the figure, similar to other vector formats. This one shouldn't loose any quality when saved, nor should it look different when reopened, but it can only be viewed in Matlab.
Personally, my workflow for figures is to do most of the drawing in Matlab programmatically and then to finalise figures by exporting to .svg and editing with Inkscape (which is open source and free) before exporting the actual size, raster versions from Inkscape. Doing as much as possible in Matlab is important, because it limits the amount of manual editing that has to be done each time a new version of the figure is produced.

hgx, is designed to quickly save the current figure in a selection of useful file formats. It just needs a filename (with or without extension) to save files to the current folder. Combined with ng and og, an example use would be:

og % Check OpenGl harware rendering is on
plot(x, y) % Plot the data
ng % Apply default template to make figure look nicer
hgx('Data') % Quickly save figure in .fig, .png, and .svg format.

Code run through

function hgx(varargin)

The inputs can either be a figure handle and a file name, or just a file name (string). If only a filename is supplied, the current figure is saved.

% Check all input arguements given
for ai = 1:length(varargin)
    switch class(varargin{ai})
        case 'matlab.ui.Figure'
            % Save any handles to h
            h = varargin{ai};
        case 'char'
            % Save any string to fnG (filename)
            fnG = varargin{ai};
    end
end

The first block of code checks the inputs, extracts them from varargin (which is a cell array containing each input). If one of the cells contains a figure handles it's assigned to the variable h to use later. The file name is saved in the variable fnG.

% If handle not specified, get current figure
if ~exist('h', 'var')
    figHandles = get(0,'Children');
    h = figHandles(1);
end

Next, the function checks to see if h exists. If a handle wasn't specified when the function was called, it won't exist yet. If it doesn't exist, the handle for the current figure is found. 

if strcmp(fnG(end-3:end), '.png')
    % Just .png using hgexport
    hgexport(h, fnG, hgexport('factorystyle'), 'Format', 'png');
elseif strcmp(fnG(end-3:end), '.fig') || strcmp(fnG(end-3:end), '.svg')
    % Just .fig/.svg using saveas 
    saveas(h, fnG);
else
    % Assume no extentsion, export .png and .fig
    hgexport(h, [fnG, '.png'], hgexport('factorystyle'), 'Format', 'png');
    saveas(h, [fnG, '.fig']);
    saveas(h, [fnG, '.svg']);
end

Finally the saving is done. fnG is checked to see if it contains .png or .fig or .svg as an extension. If one of these is found, a single save operation is performed. Matlab's function saveas is used to save .fig and .svg files, hgexport is used to save .png. Alternatively, if no format has been specified, all three formats are saved.

No comments:

AdSense