Figures are not saving as the correct size when put into a PDF
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Please, I would appreciate any help as I have been stuck on this question for days. I have coded a loop that takes images from a folder and puts them in a 2x2 table and automatically numbers them. It creates a figure with the four images on it. It repeats this process until all the images have been used. Then it creates a PDF with all the figures saved, e.g. one figure of four images per page. My issue is, I need the figures to be in the centre of a landscape page, with white space margins around this. Currently, the images look fine on the figure display when you run the code, but when I open up the PDF, they take up the entire page and leave no margin space. I'll insert two images of what I currently get, and what I need to produce. I would be so so appreciative of anyone that can help me please. Each image needs to be 10cm width by 7cm height on a landscape page in the final PDF.
Coding is not my strongest suit so I apologise if I have mistakenly used the wrong terminology. Thank you in advance.
Here's the relevant parts of my code.
% Define the desired figure size in inches
figureWidth = 11.7; % Total width of the figure in inches
figureHeight = 8.3; % Total height of the figure in inches
% Define the desired image size in centimeters
imageWidth_cm = 10; % Width of each image in centimeters
imageHeight_cm = 7.5; % Height of each image in centimeters
% Convert image size from centimeters to inches
imageWidth_inch = imageWidth_cm / 2.54; % Convert cm to inches
imageHeight_inch = imageHeight_cm / 2.54; % Convert cm to inches
% Define the spacing between images (in inches)
horizontalSpacing = 0.5; % Spacing between images horizontally
verticalSpacing = 0.5; % Spacing between images vertically
% Calculate the total width and height required for the 2x2 grid
totalWidth = 2 * imageWidth_inch + horizontalSpacing;
totalHeight = 2 * imageHeight_inch + verticalSpacing;
% Calculate the left and bottom margins to center the grid
leftMargin = (figureWidth - totalWidth) / 2;
bottomMargin = (figureHeight - totalHeight) / 2;
% Initialize a cell array to store figure handles
figures = cell(numTables, 1);
% Calculate the position of the subplot
subplotX = leftMargin + mod(i - 1, 2) * (imageWidth_inch + horizontalSpacing);
subplotY = bottomMargin + floor((i - 1) / 2) * (imageHeight_inch + verticalSpacing);
% Create a subplot for each image
axes('Position', [subplotX / figureWidth, subplotY / figureHeight, imageWidth_inch / figureWidth, imageHeight_inch / figureHeight]);
imshow(img);
axis off;
% Save the figure as a high-resolution image
outputFileName = fullfile(outputFolder, ['Figure_', num2str(tableIdx), '.png']);
exportgraphics(fig, outputFileName, 'Resolution', 300);
outputPDF = fullfile(outputFolder, 'output.pdf'); % Define the output PDF file
for i = 1:numTables
% Export the figure to the PDF
exportgraphics(figures{i}, outputPDF, 'Resolution', 300, 'Append', i > 1, 'ContentType', 'vector', 'Padding', 0.5);
end
0 commentaires
Réponses (2)
Pratyush Swain
le 17 Mar 2025
Hi Georia,
I feel the issue is that when you export the figure with 'exportgraphics' function, it defaults to filling the entire page without margins around the figures.
You can try with the following workarounds:
1 - You can use 'padding' in the exportgraphics option, but it is available only in Matlab Online since R2024a. For example:
exportgraphics(ax,"figurepadding.pdf","Padding",30)
For more information, refer to to this thread: https://www.mathworks.com/matlabcentral/answers/861570-exportgraphics-adding-a-margin and documentation: https://www.mathworks.com/help/matlab/ref/exportgraphics.html
2 - You can try setting figure properties for exporting before calculating subplots:
% For each figure in the main loop %
fig = figure;
set(fig, 'Units', 'inches', 'Position', [0, 0, figureWidth, figureHeight]);
set(fig, 'PaperUnits', 'inches', 'PaperSize', [figureWidth, figureHeight]);
set(fig, 'PaperPositionMode', 'manual', 'PaperPosition', [0, 0, figureWidth, figureHeight]);
For more information on 'Printing and Exporting' options for figure such as 'PaperSize','PaperUnits','PaperPosition', please refer to https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#d126e529412
Hope this helps.
2 commentaires
Epsilon
le 17 Mar 2025
Modifié(e) : Epsilon
le 17 Mar 2025
Hi Georia,
The units for the 'Padding' argument in the exportgraphics function default to 'points' when the ContentType is set to vector.
In the call to the exportgraphics function, the above code specifies the 'ContentType' as 'vector'. Consequently, the 'Padding' unit defaults to 'points', where 1 point equals 1/72 of an inch. This results in a padding width of 1/144 inches, which is not visible in the attached output file, even though it is actually present.
The exported padding will match the figure's padding if you set the 'Padding' argument to 'figure'. To apply a custom padding of 0.5 inches, set the 'Units' of exportgraphics to 'inches'.
%Passing the value 'figure' to the 'Padding' argument
exportgraphics(figures{i}, outputPDF, 'Resolution', 300, 'Append', i > 1, ...
'ContentType', 'vector', 'Padding', 'figure');
%or
%Setting the 'units' of exportgraphics to 'inches'
exportgraphics(figures{i}, outputPDF, 'Resolution', 300, 'Append', i > 1, ...
'ContentType', 'vector', 'Padding', 0.5,'Units','inches');
2 commentaires
Epsilon
le 19 Mar 2025
Yes it is available on MATLAB Online as well. Link to the documentation pointing to the same: https://www.mathworks.com/help/matlab/ref/exportgraphics.html#:~:text=R2024a%3A%20Specify%20dimensions%20and%20padding%20in%20MATLAB%20Online
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!