Ffigure saved as pdf: how to get proper control of margins

Hello,
I want to save a figure as a pdf, and would like to get control on the margins around the figure in the pdf.
Here is an example :
load('test_0.mat'); % contains the variables AudioSnip, frameSize, Hop , fs
s_length = length(AudioSnip);
nframes = 1 + floor((s_length - frameSize)/double(Hop ));
UsableSigLength = frameSize + floor(Hop *floor((s_length - frameSize)/double(Hop )));
sampleTime = ( 1: UsableSigLength )/fs;
%$$$$$$$$$$$$$$$$$$$
[B,f,T] = specgram(AudioSnip,frameSize*2,fs,hanning(frameSize), Hop); %(1: UsableSigLength)
B = 20*log10(abs(B));
%
TheFig = figure;
h = gcf ; %gcf returns the current figure handle
h.Units = 'centimeters';
h.PaperType= 'A4';
h.PaperUnits = 'centimeters';
h.PaperOrientation= 'landscape'; % 'portrait';
h.PaperPositionMode= 'auto';
h.InvertHardcopy = 'on';
h.Renderer = 'painter';
margin = 4.; width = 29.7; height = 21.;
h.Position = [margin margin width-2*margin height-2*margin];
h.PaperSize = [29.7 21.0]; %[21.0000 29.7000];
subplot(2,1,1);
plot(sampleTime, AudioSnip(1: UsableSigLength));
xlabel('Time (s)','FontSize', 10, 'FontName' ,'Times', 'FontWeight', 'bold');
ylabel('Amplitude', 'FontSize', 12, 'FontName' ,'Times', 'FontWeight', 'bold');
set(get(gca, 'xlabel'), 'Position', [ 21.,-0.21,-1]);
h1 = gcf;
h1.Units = 'centimeters';
atx1= gca; % axes 'Position', % xtart, ystart xend yend coord
atx1.Position = [0.1300 0.7099 0.6949 0.1635]; %
atx1.FontSize = 10 ; atx1.FontName = 'Times'; atx1.FontWeight = 'bold';
atx1.Units='normalized';
Tt0=text('Units','normalized','Position',[ 0.0009 1.4599 0 ],...
'VerticalAlignment', 'Bottom', 'FontName', 'Times', 'FontSize',14 , 'FontWeight', 'bold', 'String', 'text1');
Tt1=text('Units','normalized','Position',[ 0.0009 1.260, 0 ],...
'VerticalAlignment', 'Bottom', 'FontName', 'Times', 'FontSize',14 , 'FontWeight', 'bold', 'FontAngle', 'italic','String', 'text2');
Tt2=text('Units','normalized','Position',[0.0009 1.0509 0],...
'VerticalAlignment', 'Bottom', 'FontName', 'Times', 'FontSize',12 , 'FontWeight', 'bold', 'String', 'text3');
subplot(2,1,2);
h2 = gcf;
h2.Units = 'centimeters';
atx2= gca; % axes('Position', % xtart, ystart xend yend coord
atx2.FontSize = 10; atx2.FontName= 'Times' ; atx2.FontWeight = 'bold';
atx2.Units='normalized';
atx2.Position = [0.1300 0.0720 0.7334 0.5965 ];
imagesc(T,f,B);axis xy;colorbar;
ylabel('Frequency (Hz)','FontSize', 12, 'FontName' ,'Times', 'FontWeight', 'bold');
xlabel('','FontSize', 12, 'FontName' ,'Times', 'FontWeight', 'bold');
colormap jet
exportgraphics(TheFig, 'outputTest.pdf');
When I run it,
A) I do not get a proper control on the margins around the figure: the parameter 'margin' (see code)does not seem to affect the result in the pdf obtained
What should be done ?
Miscellaneaous other problems:
B) I would like to align the time corresponding to the end of the data (first subplot), with the end of the spectrogram data (2nd subplot): how should this be done ? (I tried some adjustment via axis position, but the colorbar is also messing things up)
C) the font for the 2nd subplot is not the expected 'Times'; (I tested that it is indeed possible to manually force 'Times' using the figure inspector, but not by the program line above (atx2.FontName = 'Times')
why ? how to enforce 'Times' by programing ?

Answers (2)

I understand that you wish to make various modifications to how the figure is being saved as a pdf. You can consider the following changes:
  1. To control the margins around the figure in pdf:
  • From MATLAB R2024a onwards, various properties like 'Padding', 'Width', 'Height', 'PreservedAspectRatio' are available with the 'exportgraphics' function. Hence upgrading to a MATLAB version from R2024a onwards would enable in setting the required margins around the figure.
  • The 'print' function can also be used instead of 'exportgraphics'. This function requires a figure object, and the properties of the figure like 'PaperSize', 'PaperPosition' etc. can be set accordingly.
2. Align Time Axis of subplots
  • You can use the 'linkaxes' function to align the axis of both subplots.
  • The 'Location' property of 'colorbar' can be used to align it properly to not interfere with the axis.
  • The 'titledlayout' function may also be useful in this case.
3. Set font for a particular plot
  • The fonts for Labels and colorbar can be set individually using their 'FontName' property. Here is an example:
set(atx2, 'FontName', 'Times');
atx2.XLabel.FontName = 'Times';
atx2.YLabel.FontName = 'Times';
c = colorbar;
c.FontName = 'Times';
The following MathWorks documentations can be referred to know more:
Thanks.
Daniel
Daniel on 19 Jan 2026
Edited: Daniel on 19 Jan 2026
I have two methods to do this. The first is easier and requires recent Matlab version with tiledlayout. I use both to set the aspect ratio and scale to exactly fit the desired output size before exporting to pdf/svg for Typst (and previously Latex) reports. The nice thing with this is that I get 1:1 scale on text and all the other aspects in the figure, i.e. the Matlab plot reflects the size in the report.
1. Tiledlayout method - from MATLAB R2024a
Set up the figure with 'PaperSize', 'PaperPosition' and use tiledlayout with its various options of padding and constraints. This is easy and works most of the time. See:
2. Manual method
This is based on hands on setting and was needed prior to tiledlayout was introduced. I still use it at times since it gives full control and allows you to manipulate the axes, subplots, colorbars, etc however you prefer.
  • First create a figure with predfined PaperSize and PaperPosition.
  • Thereafter define a function that sets OuterPosition, Position, and tightPosition. See link. You need to get these three "dimensions" right with respect to each other - and what you want to achieve though. The documentation on this is a bit better nowadays compared to the past.

Categories

Products

Release

R2021a

Asked:

on 24 Mar 2022

Edited:

on 19 Jan 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!