📄 exportfig.m
字号:
function varargout = exportfig(varargin)
%EXPORTFIG Export a figure.
% EXPORTFIG(H, FILENAME) writes the figure H to FILENAME. H is
% a figure handle and FILENAME is a string that specifies the
% name of the output file.
%
% EXPORTFIG(H, FILENAME, OPTIONS) writes the figure H to FILENAME
% with options initially specified by the structure OPTIONS. The
% field names of OPTIONS must be legal parameters listed below
% and the field values must be legal values for the corresponding
% parameter. Default options can be set in releases prior to R12
% by storing the OPTIONS structure in the root object's appdata
% with the command
% setappdata(0,'exportfigdefaults', OPTIONS)
% and for releases after R12 by setting the preference with the
% command
% setpref('exportfig', 'defaults', OPTIONS)
%
% EXPORTFIG(...,PARAM1,VAL1,PARAM2,VAL2,...) specifies
% parameters that control various characteristics of the output
% file. Any parameter value can be the string 'auto' which means
% the parameter uses the default factory behavior, overriding
% any other default for the parameter.
%
% Format Paramter:
% 'Format' a string
% specifies the output format. Defaults to 'eps'. For a
% list of export formats type 'help print'.
% 'Preview' one of the strings 'none', 'tiff'
% specifies a preview for EPS files. Defaults to 'none'.
%
% Size Parameters:
% 'Width' a positive scalar
% specifies the width in the figure's PaperUnits
% 'Height' a positive scalar
% specifies the height in the figure's PaperUnits
% 'Bounds' one of the strings 'tight', 'loose'
% specifies a tight or loose bounding box. Defaults to 'tight'.
% 'Reference' an axes handle or a string
% specifies that the width and height parameters
% are relative to the given axes. If a string is
% specified then it must evaluate to an axes handle.
%
% Specifying only one dimension sets the other dimension
% so that the exported aspect ratio is the same as the
% figure's or reference axes' current aspect ratio.
% If neither dimension is specified the size defaults to
% the width and height from the figure's or reference
% axes' size. Tight bounding boxes are only computed for
% 2-D views and in that case the computed bounds enclose all
% text objects.
%
% Rendering Parameters:
% 'Color' one of the strings 'bw', 'gray', 'cmyk'
% 'bw' specifies that lines and text are exported in
% black and all other objects in grayscale
% 'gray' specifies that all objects are exported in grayscale
% 'rgb' specifies that all objects are exported in color
% using the RGB color space
% 'cmyk' specifies that all objects are exported in color
% using the CMYK color space
% 'Renderer' one of 'painters', 'zbuffer', 'opengl'
% specifies the renderer to use
% 'Resolution' a positive scalar
% specifies the resolution in dots-per-inch.
% 'LockAxes' one of 0 or 1
% specifies that all axes limits and ticks should be fixed
% while exporting.
%
% The default color setting is 'bw'.
%
% Font Parameters:
% 'FontMode' one of the strings 'scaled', 'fixed'
% 'FontSize' a positive scalar
% in 'scaled' mode multiplies with the font size of each
% text object to obtain the exported font size
% in 'fixed' mode specifies the font size of all text
% objects in points
% 'DefaultFixedFontSize' a positive scalar
% in 'fixed' mode specified the default font size in
% points
% 'FontSizeMin' a positive scalar
% specifies the minimum font size allowed after scaling
% 'FontSizeMax' a positive scalar
% specifies the maximum font size allowed after scaling
% 'FontEncoding' one of the strings 'latin1', 'adobe'
% specifies the character encoding of the font
% 'SeparateText' one of 0 or 1
% specifies that the text objects are stored in separate
% file as EPS with the base filename having '_t' appended.
%
% If FontMode is 'scaled' but FontSize is not specified then a
% scaling factor is computed from the ratio of the size of the
% exported figure to the size of the actual figure.
%
% The default 'FontMode' setting is 'scaled'.
%
% Line Width Parameters:
% 'LineMode' one of the strings 'scaled', 'fixed'
% 'LineWidth' a positive scalar
% 'DefaultFixedLineWidth' a positive scalar
% 'LineWidthMin' a positive scalar
% specifies the minimum line width allowed after scaling
% 'LineWidthMax' a positive scalar
% specifies the maximum line width allowed after scaling
% The semantics of 'Line' parameters are exactly the
% same as the corresponding 'Font' parameters, except that
% they apply to line widths instead of font sizes.
%
% Style Map Parameter:
% 'LineStyleMap' one of [], 'bw', or a function name or handle
% specifies how to map line colors to styles. An empty
% style map means styles are not changed. The style map
% 'bw' is a built-in mapping that maps lines with the same
% color to the same style and otherwise cycles through the
% available styles. A user-specified map is a function
% that takes as input a cell array of line objects and
% outputs a cell array of line style strings. The default
% map is [].
%
% Examples:
% exportfig(gcf,'fig1.eps','height',3);
% Exports the current figure to the file named 'fig1.eps' with
% a height of 3 inches (assuming the figure's PaperUnits is
% inches) and an aspect ratio the same as the figure's aspect
% ratio on screen.
%
% opts = struct('FontMode','fixed','FontSize',10,'height',3);
% exportfig(gcf, 'fig2.eps', opts, 'height', 5);
% Exports the current figure to 'fig2.eps' with all
% text in 10 point fonts and with height 5 inches.
%
% See also PREVIEWFIG, APPLYTOFIG, RESTOREFIG, PRINT.
% Copyright 2000 Ben Hinkle
% Email bug reports and comments to bhinkle@mathworks.com
if (nargin < 2)
error('Too few input arguments');
end
% exportfig(H, filename, [options,] ...)
H = varargin{1};
if ~LocalIsHG(H,'figure')
error('First argument must be a handle to a figure.');
end
filename = varargin{2};
if ~ischar(filename)
error('Second argument must be a string.');
end
paramPairs = {varargin{3:end}};
if nargin > 2
if isstruct(paramPairs{1})
pcell = LocalToCell(paramPairs{1});
paramPairs = {pcell{:}, paramPairs{2:end}};
end
end
verstr = version;
majorver = str2num(verstr(1));
defaults = [];
if majorver > 5
if ispref('exportfig','defaults')
defaults = getpref('exportfig','defaults');
end
elseif exist('getappdata')
defaults = getappdata(0,'exportfigdefaults');
end
if ~isempty(defaults)
dcell = LocalToCell(defaults);
paramPairs = {dcell{:}, paramPairs{:}};
end
% Do some validity checking on param-value pairs
if (rem(length(paramPairs),2) ~= 0)
error(['Invalid input syntax. Optional parameters and values' ...
' must be in pairs.']);
end
auto.format = 'eps';
auto.preview = 'none';
auto.width = -1;
auto.height = -1;
auto.color = 'bw';
auto.defaultfontsize=10;
auto.fontsize = -1;
auto.fontmode='scaled';
auto.fontmin = 8;
auto.fontmax = 60;
auto.defaultlinewidth = 1.0;
auto.linewidth = -1;
auto.linemode=[];
auto.linemin = 0.5;
auto.linemax = 100;
auto.fontencoding = 'latin1';
auto.renderer = [];
auto.resolution = [];
auto.stylemap = [];
auto.applystyle = 0;
auto.refobj = -1;
auto.bounds = 'tight';
explicitbounds = 0;
auto.lockaxes = 1;
auto.separatetext = 0;
opts = auto;
% Process param-value pairs
args = {};
for k = 1:2:length(paramPairs)
param = lower(paramPairs{k});
if ~ischar(param)
error('Optional parameter names must be strings');
end
value = paramPairs{k+1};
switch (param)
case 'format'
opts.format = LocalCheckAuto(lower(value),auto.format);
if strcmp(opts.format,'preview')
error(['Format ''preview'' no longer supported. Use PREVIEWFIG' ...
' instead.']);
end
case 'preview'
opts.preview = LocalCheckAuto(lower(value),auto.preview);
if ~strcmp(opts.preview,{'none','tiff'})
error('Preview must be ''none'' or ''tiff''.');
end
case 'width'
opts.width = LocalToNum(value, auto.width);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.width)
error('Width must be a numeric scalar > 0');
end
end
case 'height'
opts.height = LocalToNum(value, auto.height);
if ~ischar(value) | ~strcmp(value,'auto')
if(~LocalIsPositiveScalar(opts.height))
error('Height must be a numeric scalar > 0');
end
end
case 'color'
opts.color = LocalCheckAuto(lower(value),auto.color);
if ~strcmp(opts.color,{'bw','gray','rgb','cmyk'})
error('Color must be ''bw'', ''gray'',''rgb'' or ''cmyk''.');
end
case 'fontmode'
opts.fontmode = LocalCheckAuto(lower(value),auto.fontmode);
if ~strcmp(opts.fontmode,{'scaled','fixed'})
error('FontMode must be ''scaled'' or ''fixed''.');
end
case 'fontsize'
opts.fontsize = LocalToNum(value,auto.fontsize);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.fontsize)
error('FontSize must be a numeric scalar > 0');
end
end
case 'defaultfixedfontsize'
opts.defaultfontsize = LocalToNum(value,auto.defaultfontsize);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.defaultfontsize)
error('DefaultFixedFontSize must be a numeric scalar > 0');
end
end
case 'fontsizemin'
opts.fontmin = LocalToNum(value,auto.fontmin);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.fontmin)
error('FontSizeMin must be a numeric scalar > 0');
end
end
case 'fontsizemax'
opts.fontmax = LocalToNum(value,auto.fontmax);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.fontmax)
error('FontSizeMax must be a numeric scalar > 0');
end
end
case 'fontencoding'
opts.fontencoding = LocalCheckAuto(lower(value),auto.fontencoding);
if ~strcmp(opts.fontencoding,{'latin1','adobe'})
error('FontEncoding must be ''latin1'' or ''adobe''.');
end
case 'linemode'
opts.linemode = LocalCheckAuto(lower(value),auto.linemode);
if ~strcmp(opts.linemode,{'scaled','fixed'})
error('LineMode must be ''scaled'' or ''fixed''.');
end
case 'linewidth'
opts.linewidth = LocalToNum(value,auto.linewidth);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.linewidth)
error('LineWidth must be a numeric scalar > 0');
end
end
case 'defaultfixedlinewidth'
opts.defaultlinewidth = LocalToNum(value,auto.defaultlinewidth);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.defaultlinewidth)
error(['DefaultFixedLineWidth must be a numeric scalar >' ...
' 0']);
end
end
case 'linewidthmin'
opts.linemin = LocalToNum(value,auto.linemin);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.linemin)
error('LineWidthMin must be a numeric scalar > 0');
end
end
case 'linewidthmax'
opts.linemax = LocalToNum(value,auto.linemax);
if ~ischar(value) | ~strcmp(value,'auto')
if ~LocalIsPositiveScalar(opts.linemax)
error('LineWidthMax must be a numeric scalar > 0');
end
end
case 'linestylemap'
opts.stylemap = LocalCheckAuto(value,auto.stylemap);
case 'renderer'
opts.renderer = LocalCheckAuto(lower(value),auto.renderer);
if ~ischar(value) | ~strcmp(value,'auto')
if ~strcmp(opts.renderer,{'painters','zbuffer','opengl'})
error(['Renderer must be ''painters'', ''zbuffer'' or' ...
' ''opengl''.']);
end
end
case 'resolution'
opts.resolution = LocalToNum(value,auto.resolution);
if ~ischar(value) | ~strcmp(value,'auto')
if ~(isnumeric(value) & (prod(size(value)) == 1) & (value >= 0));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -