]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/saveas.m
update packages
[CreaPhase.git] / octave_packages / m / plot / saveas.m
1 ## Copyright (C) 2010-2012 Kai Habel
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} saveas (@var{h}, @var{filename})
21 ## @deftypefnx {Function File} {} saveas (@var{h}, @var{filename}, @var{fmt})
22 ## Save graphic object @var{h} to the file @var{filename} in graphic
23 ## format @var{fmt}.
24 ##
25 ## @var{fmt} should be one of the following formats:
26 ##
27 ## @table @code
28 ##   @item ps
29 ##     Postscript
30 ##
31 ##   @item eps
32 ##     Encapsulated Postscript
33 ##
34 ##   @item jpg
35 ##     JPEG Image
36 ##
37 ##   @item png
38 ##     PNG Image
39 ##
40 ##   @item emf
41 ##     Enhanced Meta File
42 ##
43 ##   @item pdf
44 ##     Portable Document Format
45 ## @end table
46 ##
47 ## All device formats specified in @code{print} may also be used.  If
48 ## @var{fmt} is omitted it is extracted from the extension of @var{filename}.
49 ## The default format is @code{"pdf"}.
50 ##
51 ## @example
52 ## @group
53 ## clf ();
54 ## surf (peaks);
55 ## saveas (1, "figure1.png");
56 ## @end group
57 ## @end example
58 ##
59 ## @seealso{print}
60 ## @end deftypefn
61
62 ## Author: Kai Habel
63
64 function  saveas (h, filename, fmt = "pdf")
65
66   if ((nargin != 2) && (nargin != 3))
67     print_usage ();
68   endif
69
70   if (ishandle (h))
71     if (isfigure (h))
72       fig = h;
73     else
74       fig = ancestor (h, "figure");
75     endif
76   else
77     error ("saveas: first argument H must be a graphics handle");
78   endif
79
80   if (!ischar (filename))
81     error ("saveas: FILENAME must be a string");
82   endif
83
84   if (nargin == 2)
85     [~, ~, ext] = fileparts (filename);
86     if (!isempty (ext))
87       fmt = ext(2:end);
88     endif
89   endif
90
91   if (nargin == 3)
92     if (!ischar (filename))
93       error ("saveas: EXT must be a string");
94     endif
95
96     [~, ~, ext] = fileparts (filename);
97
98     if (isempty (ext))
99       filename = strcat (filename, ".", fmt);
100     endif
101   endif
102
103   prt_opt = strcat ("-d", tolower (fmt));
104
105   print (filename, prt_opt);
106
107 endfunction