How to make different NumberFormat in two columns (using Matlab Report Generator)
Show older comments
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);

Accepted Answer
More Answers (1)
Sameer
on 5 Jun 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!
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!