Mercurial > octave
changeset 25971:456523d3cb4a
savefig: new function
* scripts/plot/util/savefig.m: New file.
* scripts/plot/util/module.mk: Update.
* plot.txi: Document.
author | Guillaume Flandin <guillaume.offline@gmail.com> |
---|---|
date | Fri, 26 Oct 2018 16:27:36 -0400 |
parents | 3a66ae436d8a |
children | 29eb60bd2e81 |
files | doc/interpreter/plot.txi scripts/plot/util/module.mk scripts/plot/util/savefig.m |
diffstat | 3 files changed, 97 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/plot.txi Fri Oct 26 16:12:11 2018 -0400 +++ b/doc/interpreter/plot.txi Fri Oct 26 16:27:36 2018 -0400 @@ -1108,6 +1108,8 @@ @DOCSTRING(hgload) +@DOCSTRING(savefig) + @node Interacting with Plots @subsection Interacting with Plots
--- a/scripts/plot/util/module.mk Fri Oct 26 16:12:11 2018 -0400 +++ b/scripts/plot/util/module.mk Fri Oct 26 16:27:36 2018 -0400 @@ -78,6 +78,7 @@ %reldir%/rotate.m \ %reldir%/rotate3d.m \ %reldir%/saveas.m \ + %reldir%/savefig.m \ %reldir%/shg.m \ %reldir%/struct2hdl.m \ %reldir%/subplot.m \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/plot/util/savefig.m Fri Oct 26 16:27:36 2018 -0400 @@ -0,0 +1,94 @@ +## Copyright (C) 2018 Guillaume Flandin +## +## This file is part of Octave. +## +## Octave is free software: you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the filename COPYING. If not, see +## <https://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {} {} savefig () +## @deftypefnx {} {} savefig (@var{filename}) +## @deftypefnx {} {} savefig (@var{h}, @var{filename}) +## @deftypefnx {} {} savefig (@var{h}) +## @deftypefnx {} {} savefig (@var{h}, @var{filename}, @qcode{"compact"}) +## Save graphics handle @var{h} to file @var{filename}. +## +## If unspecified, @var{h} is set to the current figure. +## +## If unspecified, @var{filename} is set to @file{"Untitled.fig"}. If +## @var{filename} does not have an extension, a default extension @file{".fig"} +## will be added. +## +## A third input argument will be accepted but ignored, for Matlab compatibility. +## +## @seealso{hgsave, hdl2struct, openfig} +## @end deftypefn + +function savefig (varargin) + + ## Default values for input arguments + h = []; + filename = "Untitled.fig"; + + ## Check input arguments + if (nargin == 1) + if (ishghandle (varargin{1})) + h = varargin{1}; + elseif (ischar (varargin{1})) + filename = varargin{1}; + else + error ("savefig: invalid input"); + endif + elseif (nargin == 2 || nargin == 3) + if (! ishghandle (varargin{1})) + error ("savefig: invalid figure handle"); + endif + h = varargin{1}; + if (! ischar (varargin{2})) + error ("savefig: invalid filename"); + endif + filename = varargin{2}; + # Input "compact" ignored (Matlab compatibility) + elseif (nargin > 3) + print_usage (); + endif + + ## Check figure handle input + if (isempty (h)) + h = gcf (); + endif + + ## Check filename extension + [~, ~, ext] = fileparts (filename); + if (isempty (ext)) + filename = [filename ".fig"]; + endif + + ## Save file + hgsave (h, filename); + +endfunction + +%!test +%! unwind_protect +%! h = figure ("Visible", "off"); +%! ftmp = [tempname() ".fig"]; +%! savefig (h, ftmp); +%! savefig (ftmp); +%! savefig (h, ftmp, "compact"); +%! unwind_protect_cleanup +%! close (h); +%! unlink (ftmp); +%! end_unwind_protect