How to make different NumberFormat in two columns (using Matlab Report Generator)

2 次查看(过去 30 天)
Hello! I need to make a table in which one column has NumberFormat("%1.8f") and other has NumberFormat("%1.1f"). I can't figure it out. I tried to make 2 tables with different styles and then combine them, but it makes separate tables in word document (look at the photo). Here's the code:
tableStyles1 = { ColSep("solid"), ...
RowSep("solid"), ...
Border("solid"), ...
NumberFormat("%1.8f")};
tableStyles2 = { ColSep("solid"), ...
RowSep("solid"), ...
Border("solid"), ...
NumberFormat("%1.1f")};
Stat_table1 = FormalTable(Tide_const(:,1));
Stat_table2 = FormalTable(Tide_const(:,2));
Stat_table1.Style = [Stat_table1.Style, tableStyles1];
Stat_table2.Style = [Stat_table2.Style, tableStyles2];
Table_gen = FormalTable([Stat_table1 Stat_table2]);
append(sec1,Table_gen);

采纳的回答

Rahul Singhal
Rahul Singhal 2024-6-12
Hi Anna,
I suggest to look into the FormalTable object's ColSpecGroups property to apply column specific styles in a table.
Below is a sample code on how to define different number formattings to different columns in a table:
table = FormalTable(table_data);
grps(1) = TableColSpecGroup;
specs(1) = TableColSpec;
specs(1).Style = {NumberFormat("%1.8f")};
specs(2) = TableColSpec;
specs(2).Style = {NumberFormat("%1.1f")};
grps(1).ColSpecs = specs;
table.ColSpecGroups = grps;
Thanks,
Rahul
  1 个评论
Anna
Anna 2024-6-20
Thank you! I also found this method to be working, but yours seems to be more neat.
for iRow = 1:Stat_table.NRows
tableEntry = Stat_table.entry(iRow,2);
tableEntry.Style = [tableEntry.Style {NumberFormat("%1.8f")}];
tableEntry2 = Stat_table.entry(iRow,3);
tableEntry2.Style = [tableEntry2.Style {NumberFormat("%1.1f")}];
end
Stat_table.Style = [Stat_table.Style, tableStyles1];

请先登录,再进行评论。

更多回答(1 个)

Sameer
Sameer 2024-6-5
Hi Anna,
To create a table with different number formats for different columns, the numbers can be converted to strings with the desired format before creating the table.
Here’s how you can achieve it:
% Convert the numbers to strings with the desired format
Tide_const_str = cell(size(Tide_const));
Tide_const_str(:,1) = cellstr(num2str(Tide_const(:,1), '%1.8f'));
Tide_const_str(:,2) = cellstr(num2str(Tide_const(:,2), '%1.1f'));
% Create a single table with the formatted strings
Tide_table = FormalTable(Tide_const_str);
% Apply the styles to the table
Tide_table.TableEntriesHAlign = 'center';
Tide_table.Style = {RowSep('solid'), ColSep('solid'), Border('solid')};
append(sec1, Tide_table);
This code first converts the numbers in “Tide_const” to strings with the desired format using the “num2str” function. It then creates a “FormalTable” from these strings and applies the styles to the table. Finally, it appends the table to a section.
I hope this helps!
Sameer

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator Task Examples 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by