在MATLAB中绘图时保存轴句柄

时间:2016-03-15 17:59:21

标签: matlab matlab-figure handle axes

我有脚本处理数据,因此我可以print并在scale = 1的乳胶文档中输入它们,一切看起来都不错。

在这种情况下,我想保存图形,轴和图例句柄。

有没有办法像使用fig = figure时一样保存它们。我知道两个'黑客'

1)

nfig = nfig+1; fig = figure(nfig);
plot()
ax = gca
leg = legend()

2)

nfig = nfig+1; fig = figure(nfig);
ax = subplot(1,1,1)
plot()
leg = legend()

我的剧本

function  fig_set(fig,ax,leg,width,heigth,font_size)
%% fig_set removes borders and set witdth and height of figure
%
% The function fig_set sets the border of a figure to 0
% and set the width as a scale of the width an a4 paper and the height
% as a scale of the previously set width of the figure.
%
%   Syntax:
%       fig_set(fig,ax,leg,width,heigth,font_size)
%
%   Input:
%       - fig:          figure handl
%       - ax:           axes handle
%       - leg:          legend handle
%       - width:        width of figure, in scales of A4 paper width
%       - height:       height of figure, in scales of width of figure
%       - font_size:    font size of axes and text, it not set default=10
%
%   Output:
%       -figure of handel "fig" with set width, height and font
%       size and Interpreters set to Latex
%
% Auther:   Malthe Vibaek Eisum
% Version:  4.1
% Date:     15/3 - 2016

%% Setting font and font size
if nargin < 3
    error('need figure and axes handle')
elseif nargin < 6
    font_size = 10;
end

%% Setting figure, axes and legende Interpreters to Latex
set(fig,'DefaultTextInterpreter','Latex');
ax.TickLabelInterpreter='Latex';
if leg ~= 0
    leg.Interpreter='Latex';
end

%% Setting width and height of figure
PaperSize = get(0,'defaultFigurePaperSize');
switch fix(PaperSize(1))
    case 8
        Unit = 'inches';
    case 20
        Unit = 'centimeters';
    otherwise
        error('defaultFigurePaperSize is not equivalent to a4 paper')
end
width = PaperSize(1) * width;
height = width * heigth;

 set(fig,'Units',Unit,...
                    'Position',[2 5 width height],...
                    'PaperSize',[width height],...
                    'PaperPositionMode','auto',...
                    'Renderer','painters');
end

创建和打印图形的示例

x = 1:3;
y = rand(1,3);
nfig = nfig+1; fig=figure(nfig);
ax = gca;
plot(x,y)
xlabel('$\rho$')
ylabel('more $latex_{math}$')
leg = legend('rand');
fig_set(fig,ax,leg,1,1)
print -depsc2 myplot.eps

2 个答案:

答案 0 :(得分:1)

除非你关闭它们,否则你应该能够做到

nfig = nfig+1; 
fig{nfig} = figure(nfig);
plot()
ax{nfig} = gca
leg{nfig} = legend()

答案 1 :(得分:1)

nfig=nfig+1;                            %// raise the counter
Fig{nfig}=figure();                     %// create nfig-th figure
Ax{nfig}=axes('parent',Fig{Nfig});      %// create nfig-th axes and bind them to nfig-th figure
plot()                                  %// plot a curve
Leg{nfig}=legend(Ax{nfig},'Label1',...);%// assign nfig-th label to nfig-th axes

%% Some other code %%

get(Ax{12},'TickLabelInterpreter')      %// get the interpreter of 12-th axes (in 12-th figure)
set(Ax{5},'xlim',[-10,10])              %// set the x-limits of 5-th axes

我希望这就是你要找的东西。