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

3 次查看(过去 30 天)
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 ?

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by