📄 exportfig.m
字号:
opts.resolution = LocalToNum(value,auto.resolution);
if ~ischar(value) | ~strcmp(value,'auto')
if ~(isnumeric(value) & (prod(size(value)) == 1) & (value >= 0));
error('Resolution must be a numeric scalar >= 0');
end
end
case 'applystyle' % means to apply the options and not export
opts.applystyle = 1;
case 'reference'
if ischar(value)
if strcmp(value,'auto')
opts.refobj = auto.refobj;
else
opts.refobj = eval(value);
end
else
opts.refobj = value;
end
if ~LocalIsHG(opts.refobj,'axes')
error('Reference object must evaluate to an axes handle.');
end
case 'bounds'
opts.bounds = LocalCheckAuto(lower(value),auto.bounds);
explicitbounds = 1;
if ~strcmp(opts.bounds,{'tight','loose'})
error('Bounds must be ''tight'' or ''loose''.');
end
case 'lockaxes'
opts.lockaxes = LocalToNum(value,auto.lockaxes);
case 'separatetext'
opts.separatetext = LocalToNum(value,auto.separatetext);
otherwise
error(['Unrecognized option ' param '.']);
end
end
% make sure figure is up-to-date
drawnow;
allLines = findall(H, 'type', 'line');
allText = findall(H, 'type', 'text');
allAxes = findall(H, 'type', 'axes');
allImages = findall(H, 'type', 'image');
allLights = findall(H, 'type', 'light');
allPatch = findall(H, 'type', 'patch');
allSurf = findall(H, 'type', 'surface');
allRect = findall(H, 'type', 'rectangle');
allFont = [allText; allAxes];
allColor = [allLines; allText; allAxes; allLights];
allMarker = [allLines; allPatch; allSurf];
allEdge = [allPatch; allSurf];
allCData = [allImages; allPatch; allSurf];
old.objs = {};
old.prop = {};
old.values = {};
% Process format
if strncmp(opts.format,'eps',3) & ~strcmp(opts.preview,'none')
args = {args{:}, ['-' opts.preview]};
end
hadError = 0;
oldwarn = warning;
try
% lock axes limits, ticks and labels if requested
if opts.lockaxes
old = LocalManualAxesMode(old, allAxes, 'TickMode');
old = LocalManualAxesMode(old, allAxes, 'TickLabelMode');
old = LocalManualAxesMode(old, allAxes, 'LimMode');
end
% Process size parameters
figurePaperUnits = get(H, 'PaperUnits');
oldFigureUnits = get(H, 'Units');
oldFigPos = get(H,'Position');
set(H, 'Units', figurePaperUnits);
figPos = get(H,'Position');
refsize = figPos(3:4);
if opts.refobj ~= -1
oldUnits = get(opts.refobj, 'Units');
set(opts.refobj, 'Units', figurePaperUnits);
r = get(opts.refobj, 'Position');
refsize = r(3:4);
set(opts.refobj, 'Units', oldUnits);
end
aspectRatio = refsize(1)/refsize(2);
if (opts.width == -1) & (opts.height == -1)
opts.width = refsize(1);
opts.height = refsize(2);
elseif (opts.width == -1)
opts.width = opts.height * aspectRatio;
elseif (opts.height == -1)
opts.height = opts.width / aspectRatio;
end
wscale = opts.width/refsize(1);
hscale = opts.height/refsize(2);
sizescale = min(wscale,hscale);
old = LocalPushOldData(old,H,'PaperPositionMode', ...
get(H,'PaperPositionMode'));
set(H, 'PaperPositionMode', 'auto');
newPos = [figPos(1) figPos(2)+figPos(4)*(1-hscale) ...
wscale*figPos(3) hscale*figPos(4)];
set(H, 'Position', newPos);
set(H, 'Units', oldFigureUnits);
% process line-style map
if ~isempty(opts.stylemap) & ~isempty(allLines)
oldlstyle = LocalGetAsCell(allLines,'LineStyle');
old = LocalPushOldData(old, allLines, {'LineStyle'}, ...
oldlstyle);
newlstyle = oldlstyle;
if ischar(opts.stylemap) & strcmpi(opts.stylemap,'bw')
newlstyle = LocalMapColorToStyle(allLines);
else
try
newlstyle = feval(opts.stylemap,allLines);
catch
warning(['Skipping stylemap. ' lasterr]);
end
end
set(allLines,{'LineStyle'},newlstyle);
end
% Process rendering parameters
switch (opts.color)
case {'bw', 'gray'}
if ~strcmp(opts.color,'bw') & strncmp(opts.format,'eps',3)
opts.format = [opts.format 'c'];
end
args = {args{:}, ['-d' opts.format]};
%compute and set gray colormap
oldcmap = get(H,'Colormap');
newgrays = 0.30*oldcmap(:,1) + 0.59*oldcmap(:,2) + 0.11*oldcmap(:,3);
newcmap = [newgrays newgrays newgrays];
old = LocalPushOldData(old, H, 'Colormap', oldcmap);
set(H, 'Colormap', newcmap);
%compute and set ColorSpec and CData properties
old = LocalUpdateColors(allColor, 'color', old);
old = LocalUpdateColors(allAxes, 'xcolor', old);
old = LocalUpdateColors(allAxes, 'ycolor', old);
old = LocalUpdateColors(allAxes, 'zcolor', old);
old = LocalUpdateColors(allMarker, 'MarkerEdgeColor', old);
old = LocalUpdateColors(allMarker, 'MarkerFaceColor', old);
old = LocalUpdateColors(allEdge, 'EdgeColor', old);
old = LocalUpdateColors(allEdge, 'FaceColor', old);
old = LocalUpdateColors(allCData, 'CData', old);
case {'rgb','cmyk'}
if strncmp(opts.format,'eps',3)
opts.format = [opts.format 'c'];
args = {args{:}, ['-d' opts.format]};
if strcmp(opts.color,'cmyk')
args = {args{:}, '-cmyk'};
end
else
args = {args{:}, ['-d' opts.format]};
end
otherwise
error('Invalid Color parameter');
end
if (~isempty(opts.renderer))
args = {args{:}, ['-' opts.renderer]};
end
if (~isempty(opts.resolution)) | ~strncmp(opts.format,'eps',3)
if isempty(opts.resolution)
opts.resolution = 0;
end
args = {args{:}, ['-r' int2str(opts.resolution)]};
end
% Process font parameters
if ~isempty(opts.fontmode)
oldfonts = LocalGetAsCell(allFont,'FontSize');
oldfontunits = LocalGetAsCell(allFont,'FontUnits');
set(allFont,'FontUnits','points');
switch (opts.fontmode)
case 'fixed'
if (opts.fontsize == -1)
set(allFont,'FontSize',opts.defaultfontsize);
else
set(allFont,'FontSize',opts.fontsize);
end
case 'scaled'
if (opts.fontsize == -1)
scale = sizescale;
else
scale = opts.fontsize;
end
newfonts = LocalScale(oldfonts,scale,opts.fontmin,opts.fontmax);
set(allFont,{'FontSize'},newfonts);
otherwise
error('Invalid FontMode parameter');
end
old = LocalPushOldData(old, allFont, {'FontSize'}, oldfonts);
old = LocalPushOldData(old, allFont, {'FontUnits'}, oldfontunits);
end
if strcmp(opts.fontencoding,'adobe') & strncmp(opts.format,'eps',3)
args = {args{:}, '-adobecset'};
end
% Process line parameters
if ~isempty(opts.linemode)
oldlines = LocalGetAsCell(allMarker,'LineWidth');
old = LocalPushOldData(old, allMarker, {'LineWidth'}, oldlines);
switch (opts.linemode)
case 'fixed'
if (opts.linewidth == -1)
set(allMarker,'LineWidth',opts.defaultlinewidth);
else
set(allMarker,'LineWidth',opts.linewidth);
end
case 'scaled'
if (opts.linewidth == -1)
scale = sizescale;
else
scale = opts.linewidth;
end
newlines = LocalScale(oldlines, scale, opts.linemin, opts.linemax);
set(allMarker,{'LineWidth'},newlines);
end
end
% adjust figure bounds to surround axes
if strcmp(opts.bounds,'tight')
if (~strncmp(opts.format,'eps',3) & LocalHas3DPlot(allAxes)) | ...
(strncmp(opts.format,'eps',3) & opts.separatetext)
if (explicitbounds == 1)
warning(['Cannot compute ''tight'' bounds. Using ''loose''' ...
' bounds.']);
end
opts.bounds = 'loose';
end
end
warning('off');
if ~isempty(allAxes)
if strncmp(opts.format,'eps',3)
if strcmp(opts.bounds,'loose')
args = {args{:}, '-loose'};
end
old = LocalPushOldData(old,H,'Position', oldFigPos);
elseif strcmp(opts.bounds,'tight')
oldaunits = LocalGetAsCell(allAxes,'Units');
oldapos = LocalGetAsCell(allAxes,'Position');
oldtunits = LocalGetAsCell(allText,'units');
oldtpos = LocalGetAsCell(allText,'Position');
set(allAxes,'units','points');
apos = LocalGetAsCell(allAxes,'Position');
oldunits = get(H,'Units');
set(H,'units','points');
origfr = get(H,'position');
fr = [];
for k=1:length(allAxes)
if ~strcmpi(get(allAxes(k),'Tag'),'legend')
axesR = apos{k};
r = LocalAxesTightBoundingBox(axesR, allAxes(k));
r(1:2) = r(1:2) + axesR(1:2);
fr = LocalUnionRect(fr,r);
end
end
if isempty(fr)
fr = [0 0 origfr(3:4)];
end
for k=1:length(allAxes)
ax = allAxes(k);
r = apos{k};
r(1:2) = r(1:2) - fr(1:2);
set(ax,'Position',r);
end
old = LocalPushOldData(old, allAxes, {'Position'}, oldapos);
old = LocalPushOldData(old, allText, {'Position'}, oldtpos);
old = LocalPushOldData(old, allText, {'Units'}, oldtunits);
old = LocalPushOldData(old, allAxes, {'Units'}, oldaunits);
old = LocalPushOldData(old, H, 'Position', oldFigPos);
old = LocalPushOldData(old, H, 'Units', oldFigureUnits);
r = [origfr(1) origfr(2)+origfr(4)-fr(4) fr(3:4)];
set(H,'Position',r);
else
args = {args{:}, '-loose'};
old = LocalPushOldData(old,H,'Position', oldFigPos);
end
end
% Process text in a separate file if needed
if opts.separatetext & ~opts.applystyle
% First hide all text and export
oldtvis = LocalGetAsCell(allText,'visible');
set(allText,'visible','off');
oldax = LocalGetAsCell(allAxes,'XTickLabel',1);
olday = LocalGetAsCell(allAxes,'YTickLabel',1);
oldaz = LocalGetAsCell(allAxes,'ZTickLabel',1);
null = cell(length(oldax),1);
[null{:}] = deal([]);
set(allAxes,{'XTickLabel'},null);
set(allAxes,{'YTickLabel'},null);
set(allAxes,{'ZTickLabel'},null);
print(H, filename, args{:});
set(allText,{'Visible'},oldtvis);
set(allAxes,{'XTickLabel'},oldax);
set(allAxes,{'YTickLabel'},olday);
set(allAxes,{'ZTickLabel'},oldaz);
% Now hide all non-text and export as eps in painters
[path, name, ext] = fileparts(filename);
tfile = fullfile(path,[name '_t.eps']);
tfile2 = fullfile(path,[name '_t2.eps']);
foundRenderer = 0;
for k=1:length(args)
if strncmp('-d',args{k},2)
args{k} = '-deps';
elseif strncmp('-zbuffer',args{k},8) | ...
strncmp('-opengl', args{k},6)
args{k} = '-painters';
foundRenderer = 1;
end
end
if ~foundRenderer
args = {args{:}, '-painters'};
end
allNonText = [allLines; allLights; allPatch; ...
allImages; allSurf; allRect];
oldvis = LocalGetAsCell(allNonText,'visible');
oldc = LocalGetAsCell(allAxes,'color');
oldaxg = LocalGetAsCell(allAxes,'XGrid');
oldayg = LocalGetAsCell(allAxes,'YGrid');
oldazg = LocalGetAsCell(allAxes,'ZGrid');
[null{:}] = deal('off');
set(allAxes,{'XGrid'},null);
set(allAxes,{'YGrid'},null);
set(allAxes,{'ZGrid'},null);
set(allNonText,'Visible','off');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -