Figures are not saving as the correct size when put into a PDF
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 2×2 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 2×2 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);
endPlease, 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 2×2 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 2×2 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 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 2×2 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 2×2 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 pdf, size, image, figure, output, merge MATLAB Answers — New Questions