Change table headers font style size and make them multiple line for printing in figure Matlab 2022a.

10 次查看(过去 30 天)
Hi all,
Thanks in advance for the help.
I have created a couple of tables 'soilProfFinal' and 'soilProfFinal_LE' to which I defined defined headers, the aspect of them is in figure below.
Now I am trying to print those to PDF and already sorted the commands, see below script.
% Create a new figure
figTables = figure(i+2000);
set(gcf, 'Units', 'centimeters', 'Position', [1, 1, 21, 29.7]); % A4 size in centimeters
% Create subplot for 'soilProfFinal'
subplot(2, 1, 1); hold on; box off; axis off;
uitableObj_1 = uitable('Data', soilProfFinal{:,:}, 'ColumnName', headersSoil, 'Units', 'normalized', 'Position', [0.10, 0.525, 0.777, 0.40], 'ColumnWidth', {105, 120, 75, 150, 130});
title(['EW2-', locationCode ,' soil profile and soil properties adopted for the PDA','\newline','considering Glauconitic soil units as "Fine Grained"']); % Add a title if needed
hold off;
% Create subplot for 'soilProfFinal_LE'
subplot(2, 1, 2); hold on; box off; axis off;
uitableObj_2 = uitable('Data', soilProfFinal_LE{:,:}, 'ColumnName', headersSoil, 'Units', 'normalized', 'Position', [0.10, 0.05, 0.777, 0.40], 'ColumnWidth', {105, 120, 75, 150, 130});
title(['EW2-', locationCode ,' soil profile and soil properties adopted for the PDA','\newline','considering Glauconitic soil units as "Coarse Grained"']); % Add a title if needed
hold off;
% Print the figure to a PDF file
print(figTables,[fileName,'_1'],'-dpdf');
Now the problem is the following: when the figures are created all seems okay but when I print the headers font changes and/or does not fit the column width see figures.
Anyone knows how to fix this please? Many thanks

回答(1 个)

R
R 2023-12-22
Hi Paulino,
As far as I know, the functionality for changing the font style of “uitableheaders is not available in MATLAB R2022a. However, there are a couple workarounds given in the following MATLAB Answer:
Below is a sample code I wrote using your original script and incorporating the suggested workaround:
% Creating dummy data
soilProfFinal = array2table(rand(15,5),'VariableNames',["Top of Layer[m]", "Bottom of Layer[m]", "Soil Type","Bulk unit weight [kN/m^3]","Friction angle [deg]"]);
soilProfFinal_LE = array2table(rand(15,5),'VariableNames',["Top of Layer[m]", "Bottom of Layer[m]", "Soil Type","Bulk unit weight [kN/m^2]","Friction angle [deg]"]);
% Adding headers as the first row of the table
C1 = [soilProfFinal.Properties.VariableNames; table2cell(soilProfFinal)];
C2 = [soilProfFinal_LE.Properties.VariableNames; table2cell(soilProfFinal_LE)];
% Create a new figure
fig = figure();
set(gcf, 'Units', 'centimeters', 'Position', [1, 1, 21, 29.7]);
% Create subplot for 'soilProfFinal'
subplot(2, 1, 1); hold on; box off; axis off;
uitableObj_1 = uitable('Data', C1, 'ColumnName',{}, 'RowName',{}, 'Units', 'normalized', 'Position', [0.10, 0.525, 0.777, 0.40], 'ColumnWidth', {110, 120, 95, 155, 130});
title(['EW2-A30 soil profile and soil properties adopted for the PDA','\newline','considering Glauconitic soil units as "Fine Grained"']); % Add a title if needed
hold off;
% Create subplot for 'soilProfFinal_LE'
subplot(2, 1, 2); hold on; box off; axis off;
uitableObj_2 = uitable('Data', C1, 'ColumnName',{}, 'RowName',{}, 'Units', 'normalized', 'Position', [0.10, 0.05, 0.777, 0.40], 'ColumnWidth', {110, 120, 95, 155, 130});
title(['EW2-A30 soil profile and soil properties adopted for the PDA','\newline','considering Glauconitic soil units as "Coarse Grained"']); % Add a title if needed
hold off;
% Print the figure to a PDF file
print(fig,'tables.pdf','-dpdf');
Note that this changes how the table will be indexed since data starts at row #2. Here is the output for the above code:
Additionally, you can change the font style of the first row by using the addStyle” function.
Hope this helps!
  3 个评论
Paulino
Paulino 2024-1-10
编辑:Paulino 2024-1-10
I have another query now simple one.
I am trying to print the table image in png to pdf A4 portrait but the table is to small (see figure and pdf).
For that I have the following code:
% List of files in output
filesPNG = dir(fullfile("Best_Estimate/Report_Plots_BE/", '*.png'));
% Loop through each png file
for i = 1:3%numel(filesPNG);
% Get the current file name
pngFileName = fullfile(directory, filesPNG(i).name);
% Create a PDF file name based on the PNG file name
[~, baseName, ~] = fileparts(filesPNG(i).name);
pdfFileName = fullfile(outputDir, [baseName, '.pdf']);
% Open a new figure
figure('Visible', 'off');
% Read the PNG image
img = imread(pngFileName);
% Display the image in the figure
imshow(img);
% Set figure size to A4 dimensions (approximately)
fig.Position = [100, 100, 595, 842];
% Set figure to vertical orientation
fig.PaperOrientation = 'portrait';
% Set paper size to A4
fig.PaperSize = [595, 842];
% Set figure units to points
fig.PaperUnits = 'points';
% Set figure margins to zero
fig.PaperPositionMode = 'auto';
% Set additional margin at the bottom
fig.PaperPosition = [0, 0, 595, 842];
% Print the figure to a PDF file
print(pdfFileName, '-dpdf', '-r300');
fprintf('Converted: %s\n', pdfFileName);
end
Can you please help to increase the image and move it upwards a bit closer to the top edge of the A4 page?
Thanks a lot in advance
R
R 2024-1-18
MATLAB's report generator is quite efficient for this task. You can easily set the Page Margins and change the dimensions of your image using it. Here's the code for the same:
import mlreportgen.dom.*
d = Document('exampleTable','pdf');
open(d)
s = d.CurrentPageLayout;
s.PageMargins.Top = "0in";
s.PageSize.Orientation ='portrait';
s.PageSize.Height = '11.7in';
s.PageSize.Width = '8.3in';
img = Image(which('exampleTable.png'));
img.Style = {Height('10in'),HAlign("center"),Width('7in'),VAlign("top")};
append(d,img);
close(d);
rptview(d);
Also attaching the output here for reference.
Refer to the following documentation to know more about the report generator:
I hope this helps you resize the table!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Support 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by