Change table headers font style size and make them multiple line for printing in figure Matlab 2022a.
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
R
am 22 Dez. 2023
Hi Paulino,
As far as I know, the functionality for changing the font style of “uitable” headers 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 Kommentare
R
am 18 Jan. 2024
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:
- https://www.mathworks.com/help/rptgen/ug/mlreportgen.dom.document-class.html
- https://www.mathworks.com/help/rptgen/ug/set-page-margins-in-a-pdf-report.html
I hope this helps you resize the table!
Siehe auch
Kategorien
Mehr zu Language Support finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!