Reputation: 189
I have script handling figures so that I can print
and input them in a latex document with scale = 1 and everything looks nice.
In this context I want to save the figure, axes and legend handles.
Is there a way to save them like when using fig = figure
. I know two 'hacks'
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()
My script
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
Example of creating and printing a figure
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
Upvotes: 1
Views: 791
Reputation: 2331
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
I hope it is what you are looking for.
Upvotes: 1
Reputation: 35525
Unless you close them, you shoudl be able to do
nfig = nfig+1;
fig{nfig} = figure(nfig);
plot()
ax{nfig} = gca
leg{nfig} = legend()
Upvotes: 1