1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
function _make_figures(plot_dir, fmt, name, wid, hgt, res, start_ind)
%%%% Create figures in the given plot directory.
%%%%
handles = get(0, 'children');
for ind = 1:length(handles)
filename = sprintf('%s%03d', name, ind + start_ind);
filepath = fullfile(plot_dir, [filename, '.', fmt]);
pngpath = fullfile(plot_dir, [filename, '.png']);
h = handles(ind);
pos = get(h, 'position');
% If no width or height is given, use the figure size.
if (wid < 0 && hgt < 0 && res == 0)
wid = pos(3);
hgt = pos(4);
% If no width is given, scale based on figure aspect.
elseif (wid < 0)
wid = pos(3) * hgt / pos(4);
% If no height is given, scale based on figure aspect.
elseif (hgt < 0)
hgt = pos(4) * wid / pos(3);
end;
if (wid > 0)
size_opt = sprintf('-S%d,%d', wid, hgt);
else
size_opt = sprintf('-r%d', res);
end
% Try to use imwrite if the figure only contains an image
% with no title or labels.
im = check_imwrite(h);
if (im)
try
% Try to save the image.
save_image(im, pngpath);
catch
% Fall back on a standard figure save.
safe_print(h, filepath, pngpath, size_opt);
end;
elseif (fmt == 'svg' && strcmp(graphics_toolkit, 'gnuplot'))
% there is a bug in gnuplot 5.2 that prevents svgs from drawing.
% e.g. "no element found: line 1, column 0"
set(0, 'currentfigure', h)
drawnow('svg', filepath)
else
safe_print(h, filepath, pngpath, size_opt);
end;
close(h);
end;
end;
function im = check_imwrite(h)
im = '';
if (length(get(h, 'children')) != 1)
return;
end;
ax = get(h, 'children');
if (length(get(ax, 'children')) != 1)
return;
end;
artist = get(ax, 'children');
if (strcmp(get(artist, 'type'), 'image') != 1)
return;
end;
if (get(get(ax, 'title'), 'string'))
return;
end;
if (get(get(ax, 'xlabel'), 'string'))
return;
end;
if (get(get(ax, 'ylabel'), 'string'))
return;
end;
% Check for image too small to display
cdata = get(artist, 'cdata');
if (size(cdata)(1) < 100 || size(cdata)(2) < 100)
return;
end;
% bail if image is more than 2-dimensions
if (ndims(cdata) > 2)
return;
end
im = artist;
end;
function save_image(im, pngpath)
cdata = double(get(im, 'cdata'));
mapping = get(im, 'cdatamapping');
if (strcmp(mapping, 'scaled') == 1)
clim = get(im, 'clim');
cdata = cdata - clim(1);
cdata = cdata ./ (clim(2) - clim(1));
end;
cmap = colormap(get(im, 'parent'));
[I, ~] = gray2ind(cdata, length(cmap));
imwrite(I, cmap, pngpath);
end;
function safe_print(h, filepath, altpath, size_opt)
try
inner_print(h, filepath, size_opt);
catch
try
inner_print(h, altpath, size_opt);
catch ME
close(h);
error(ME.message);
end
end
end
function inner_print(h, filepath, size_opt)
try
print(h, filepath, size_opt);
catch
print(h, filepath);
end
end
|