Skip to content

Commit c6cdb2f

Browse files
committed
(3.46) Added -xkcd option (thanks @slayton); added .fig input and output format (previously undocumented & buggy); redirect .tex output to matlab2tikz utility
1 parent 8e811c1 commit c6cdb2f

File tree

2 files changed

+692
-5
lines changed

2 files changed

+692
-5
lines changed

export_fig.m

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
% export_fig ... -silent
3838
% export_fig ... -notify
3939
% export_fig ... -regexprep <pattern> <replace>
40+
% export_fig ... -xkcd
4041
% export_fig ... -toolbar
4142
% export_fig ... -menubar
4243
% export_fig ... -contextmenu
@@ -103,6 +104,9 @@
103104
% default name 'export_fig_out' is used. If neither file extension
104105
% nor a format parameter are specified, a ".png" is added to the
105106
% filename and the figure saved in PNG format.
107+
% Special case: if filename has .fig extension the current figure is
108+
% saved to that file in Matlab FIG format; if no file is open, the
109+
% specified file is regarded as input file, and used for re-export.
106110
% -<format> - string(s) containing the output file extension(s). Options:
107111
% '-pdf','-eps','emf','-svg','-png','-tif','-jpg','-gif' and '-bmp'.
108112
% Multiple formats can be specified, without restriction.
@@ -149,8 +153,9 @@
149153
% formats or figures with patches and/or transparent annotations;
150154
% painters for vector formats without patches/transparencies.
151155
% -<colorspace> - option indicating which colorspace color figures should
152-
% be saved in: RGB (default), CMYK or gray. Usage example: '-gray'.
153-
% Note: CMYK is only supported in PDF, EPS and TIF formats.
156+
% be saved in: RGB (default), CMYK or gray.
157+
% Usage example: '-gray' creates a grayscale version of the figure.
158+
% Note: CMYK is only supported in PDF, EPS and TIF output formats.
154159
% -q<val> - option to vary bitmap image quality (PDF, EPS, JPG formats only).
155160
% A larger val, in the range 0-100, produces higher quality and
156161
% lower compression. val > 100 results in lossless compression.
@@ -223,6 +228,7 @@
223228
% string or array of strings; case-sensitive), with the corresponding
224229
% <new> string(s), in EPS/PDF files (only). See regexp function's doc.
225230
% Warning: invalid replacement can make your EPS/PDF file unreadable!
231+
% -xkcd - renders the axes in XKCD hand-drawn style (see http://xkcd.com)
226232
% -toolbar - adds an interactive export button to the figure's toolbar
227233
% -menubar - adds an interactive export menu to the figure's menubar
228234
% -contextmenu - adds interactive export menu to figure context-menu (right-click)
@@ -392,6 +398,7 @@
392398
% 07/12/23: (3.43) Fixed unintended modification of colorbar in bitmap export (issue #385)
393399
% 21/02/24: (3.44) Fixed: text objects with normalized units were not exported in some cases (issue #373); added check for invalid ghostscript installation (issue #365)
394400
% 02/05/24: (3.45) Display the builtin error message when uifigure cannot be exported (issue #387); fixed contour labels with non-default FontName incorrectly exported as Courier (issue #388)
401+
% 09/05/24: (3.46) Added -xkcd option (thanks @slayton); added .fig input and output format (previously undocumented & buggy); redirect .tex output to matlab2tikz utility
395402
%}
396403

397404
if nargout
@@ -429,7 +436,7 @@
429436
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});
430437

431438
% Check for newer version and exportgraphics/copygraphics compatibility
432-
currentVersion = 3.45;
439+
currentVersion = 3.46;
433440
if options.version % export_fig's version requested - return it and bail out
434441
imageData = currentVersion;
435442
return
@@ -674,6 +681,11 @@
674681
try
675682
oldWarn = warning;
676683

684+
% If XKCD option was specified, render figure as XKCD before any export
685+
if options.xkcd
686+
xkcd_axes = xkcdify(fig);
687+
end
688+
677689
% Export bitmap formats first
678690
if isbitmap(options)
679691
if abs(options.bb_padding) > 1
@@ -1516,6 +1528,9 @@
15161528
end
15171529
end
15181530

1531+
% Revert any XKCD rendering
1532+
try delete(xkcd_axes); catch, end
1533+
15191534
% Notify user by popup, if -notify option was specified
15201535
if options.notify && exported_files > 0
15211536
% TODO don't notify when exporting to file just for clipboard output
@@ -1551,6 +1566,8 @@
15511566
% Revert figure properties in case they were changed
15521567
try set(fig,'Units',oldFigUnits, 'Position',pos, 'Color',tcol_orig); catch, end
15531568
try set(textn, 'Units','normalized'); catch, end
1569+
% Revert any XKCD rendering
1570+
try delete(xkcd_axes); catch, end
15541571
% Display possible workarounds before the error message
15551572
if ~isempty(regexpi(err.message,'setopacityalpha')) %#ok<RGXPI>
15561573
% Alert the user that transparency is not supported (issue #285)
@@ -1710,6 +1727,7 @@ function notify(filename)
17101727
'preserve_size', false, ...
17111728
'silent', false, ...
17121729
'notify', false, ...
1730+
'xkcd', false, ...
17131731
'regexprep', [], ...
17141732
'toolbar', false, ...
17151733
'menubar', false, ...
@@ -1735,6 +1753,7 @@ function notify(filename)
17351753
options.alpha = (nout == 2); % user requested alpha output
17361754
options.handleName = ''; % default handle name
17371755
wasOutputRequested = false;
1756+
saveFig = false;
17381757

17391758
% Go through the other arguments
17401759
skipNext = 0;
@@ -1804,6 +1823,12 @@ function notify(filename)
18041823
options.gif = true;
18051824
addToOptionsStr = false;
18061825
wasOutputRequested = true;
1826+
case 'tex'
1827+
url = hyperlink('https://github.com/matlab2tikz/matlab2tikz','matlab2tikz');
1828+
error('export_fig:TEX','export_fig does not support tex output. Use the %s utility for this.', url);
1829+
case 'fig'
1830+
saveFig = true;
1831+
addToOptionsStr = false;
18071832
case 'rgb'
18081833
options.colourspace = 0;
18091834
case 'cmyk'
@@ -1882,6 +1907,9 @@ function notify(filename)
18821907
case 'regexprep'
18831908
options.regexprep = varargin(a+1:a+2);
18841909
skipNext = 2;
1910+
case 'xkcd'
1911+
options.xkcd = true;
1912+
addToOptionsStr = false;
18851913
case 'toolbar'
18861914
options.toolbar = true;
18871915
addToOptionsStr = false;
@@ -2044,7 +2072,7 @@ function notify(filename)
20442072
case {'tif', 'tiff','jpg', 'jpeg','png','bmp','eps','emf','pdf','svg','gif'}
20452073
options = setOptionsFormat(options, ext);
20462074
wasOutputRequested = true;
2047-
case '.fig'
2075+
case 'fig'
20482076
% If no open figure, then load the specified .fig file and continue
20492077
figFilename = thisArg;
20502078
if isempty(fig)
@@ -2054,10 +2082,14 @@ function notify(filename)
20542082
options.handleName = ['openfig(''' figFilename ''')'];
20552083
else
20562084
% save the current figure as the specified .fig file and exit
2057-
saveas(fig(1),figFilename);
2085+
hFig = ancestor(fig(1), 'figure');
2086+
saveas(hFig,figFilename);
20582087
fig = -1;
20592088
return
20602089
end
2090+
case 'tex'
2091+
url = hyperlink('https://github.com/matlab2tikz/matlab2tikz','matlab2tikz');
2092+
error('export_fig:TEX','export_fig does not support tex output. Use the %s utility for this.', url);
20612093
otherwise
20622094
options.name = thisArg;
20632095
wasOutputRequested = true;
@@ -2116,6 +2148,17 @@ function notify(filename)
21162148
options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end));
21172149
end
21182150

2151+
% Export the current figure without any manipulation, if requested
2152+
if saveFig
2153+
if ~isempty(options.name)
2154+
[fpath,fname,~] = fileparts(options.name);
2155+
filename = fullfile(fpath,[fname '.fig']);
2156+
else
2157+
filename = 'output.fig';
2158+
end
2159+
saveas(ancestor(fig(1),'figure'), filename);
2160+
end
2161+
21192162
% Compute the magnification and resolution
21202163
if isempty(options.magnify)
21212164
if isempty(options.resolution)

0 commit comments

Comments
 (0)