Skip to content

Commit

Permalink
(3.46) Added -xkcd option (thanks @slayton); added .fig input and out…
Browse files Browse the repository at this point in the history
…put format (previously undocumented & buggy); redirect .tex output to matlab2tikz utility
  • Loading branch information
altmany committed May 9, 2024
1 parent 8e811c1 commit c6cdb2f
Show file tree
Hide file tree
Showing 2 changed files with 692 additions and 5 deletions.
53 changes: 48 additions & 5 deletions export_fig.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
% export_fig ... -silent
% export_fig ... -notify
% export_fig ... -regexprep <pattern> <replace>
% export_fig ... -xkcd
% export_fig ... -toolbar
% export_fig ... -menubar
% export_fig ... -contextmenu
Expand Down Expand Up @@ -103,6 +104,9 @@
% default name 'export_fig_out' is used. If neither file extension
% nor a format parameter are specified, a ".png" is added to the
% filename and the figure saved in PNG format.
% Special case: if filename has .fig extension the current figure is
% saved to that file in Matlab FIG format; if no file is open, the
% specified file is regarded as input file, and used for re-export.
% -<format> - string(s) containing the output file extension(s). Options:
% '-pdf','-eps','emf','-svg','-png','-tif','-jpg','-gif' and '-bmp'.
% Multiple formats can be specified, without restriction.
Expand Down Expand Up @@ -149,8 +153,9 @@
% formats or figures with patches and/or transparent annotations;
% painters for vector formats without patches/transparencies.
% -<colorspace> - option indicating which colorspace color figures should
% be saved in: RGB (default), CMYK or gray. Usage example: '-gray'.
% Note: CMYK is only supported in PDF, EPS and TIF formats.
% be saved in: RGB (default), CMYK or gray.
% Usage example: '-gray' creates a grayscale version of the figure.
% Note: CMYK is only supported in PDF, EPS and TIF output formats.
% -q<val> - option to vary bitmap image quality (PDF, EPS, JPG formats only).
% A larger val, in the range 0-100, produces higher quality and
% lower compression. val > 100 results in lossless compression.
Expand Down Expand Up @@ -223,6 +228,7 @@
% string or array of strings; case-sensitive), with the corresponding
% <new> string(s), in EPS/PDF files (only). See regexp function's doc.
% Warning: invalid replacement can make your EPS/PDF file unreadable!
% -xkcd - renders the axes in XKCD hand-drawn style (see http://xkcd.com)
% -toolbar - adds an interactive export button to the figure's toolbar
% -menubar - adds an interactive export menu to the figure's menubar
% -contextmenu - adds interactive export menu to figure context-menu (right-click)
Expand Down Expand Up @@ -392,6 +398,7 @@
% 07/12/23: (3.43) Fixed unintended modification of colorbar in bitmap export (issue #385)
% 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)
% 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)
% 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
%}

if nargout
Expand Down Expand Up @@ -429,7 +436,7 @@
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});

% Check for newer version and exportgraphics/copygraphics compatibility
currentVersion = 3.45;
currentVersion = 3.46;
if options.version % export_fig's version requested - return it and bail out
imageData = currentVersion;
return
Expand Down Expand Up @@ -674,6 +681,11 @@
try
oldWarn = warning;

% If XKCD option was specified, render figure as XKCD before any export
if options.xkcd
xkcd_axes = xkcdify(fig);
end

% Export bitmap formats first
if isbitmap(options)
if abs(options.bb_padding) > 1
Expand Down Expand Up @@ -1516,6 +1528,9 @@
end
end

% Revert any XKCD rendering
try delete(xkcd_axes); catch, end

% Notify user by popup, if -notify option was specified
if options.notify && exported_files > 0
% TODO don't notify when exporting to file just for clipboard output
Expand Down Expand Up @@ -1551,6 +1566,8 @@
% Revert figure properties in case they were changed
try set(fig,'Units',oldFigUnits, 'Position',pos, 'Color',tcol_orig); catch, end
try set(textn, 'Units','normalized'); catch, end
% Revert any XKCD rendering
try delete(xkcd_axes); catch, end
% Display possible workarounds before the error message
if ~isempty(regexpi(err.message,'setopacityalpha')) %#ok<RGXPI>
% Alert the user that transparency is not supported (issue #285)
Expand Down Expand Up @@ -1710,6 +1727,7 @@ function notify(filename)
'preserve_size', false, ...
'silent', false, ...
'notify', false, ...
'xkcd', false, ...
'regexprep', [], ...
'toolbar', false, ...
'menubar', false, ...
Expand All @@ -1735,6 +1753,7 @@ function notify(filename)
options.alpha = (nout == 2); % user requested alpha output
options.handleName = ''; % default handle name
wasOutputRequested = false;
saveFig = false;

% Go through the other arguments
skipNext = 0;
Expand Down Expand Up @@ -1804,6 +1823,12 @@ function notify(filename)
options.gif = true;
addToOptionsStr = false;
wasOutputRequested = true;
case 'tex'
url = hyperlink('https://github.com/matlab2tikz/matlab2tikz','matlab2tikz');
error('export_fig:TEX','export_fig does not support tex output. Use the %s utility for this.', url);
case 'fig'
saveFig = true;
addToOptionsStr = false;
case 'rgb'
options.colourspace = 0;
case 'cmyk'
Expand Down Expand Up @@ -1882,6 +1907,9 @@ function notify(filename)
case 'regexprep'
options.regexprep = varargin(a+1:a+2);
skipNext = 2;
case 'xkcd'
options.xkcd = true;
addToOptionsStr = false;
case 'toolbar'
options.toolbar = true;
addToOptionsStr = false;
Expand Down Expand Up @@ -2044,7 +2072,7 @@ function notify(filename)
case {'tif', 'tiff','jpg', 'jpeg','png','bmp','eps','emf','pdf','svg','gif'}
options = setOptionsFormat(options, ext);
wasOutputRequested = true;
case '.fig'
case 'fig'
% If no open figure, then load the specified .fig file and continue
figFilename = thisArg;
if isempty(fig)
Expand All @@ -2054,10 +2082,14 @@ function notify(filename)
options.handleName = ['openfig(''' figFilename ''')'];
else
% save the current figure as the specified .fig file and exit
saveas(fig(1),figFilename);
hFig = ancestor(fig(1), 'figure');
saveas(hFig,figFilename);
fig = -1;
return
end
case 'tex'
url = hyperlink('https://github.com/matlab2tikz/matlab2tikz','matlab2tikz');
error('export_fig:TEX','export_fig does not support tex output. Use the %s utility for this.', url);
otherwise
options.name = thisArg;
wasOutputRequested = true;
Expand Down Expand Up @@ -2116,6 +2148,17 @@ function notify(filename)
options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end));
end

% Export the current figure without any manipulation, if requested
if saveFig
if ~isempty(options.name)
[fpath,fname,~] = fileparts(options.name);
filename = fullfile(fpath,[fname '.fig']);
else
filename = 'output.fig';
end
saveas(ancestor(fig(1),'figure'), filename);
end

% Compute the magnification and resolution
if isempty(options.magnify)
if isempty(options.resolution)
Expand Down
Loading

0 comments on commit c6cdb2f

Please sign in to comment.