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

4 visualizaciones (últimos 30 días)
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);

Respuesta aceptada

Rahul Singhal
Rahul Singhal el 12 de Jun. de 2024
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 comentario
Anna
Anna el 20 de Jun. de 2024
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];

Iniciar sesión para comentar.

Más respuestas (1)

Sameer
Sameer el 5 de Jun. de 2024
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

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by