How do I control number formatting for 'mlreportgen.ppt.Table' object?
7 views (last 30 days)
Show older comments
MathWorks Support Team
on 23 Jan 2023
Edited: MathWorks Support Team
on 13 Sep 2024 at 17:47
I want to control number formatting in a 'mlreportgen.ppt.Table' object. If I have a ‘mlreportgen.dom.NumberFormat’ object, I can specify the numbers in the table to have a chosen precision using 'NumberFormat' or 'setDefaultNumberFormat', but I cannot find any way to do this for 'mlreportgen.ppt.Table' object.
I'd like to use general formatting strings like '%1.2f' or a precision spec from the 'round' function. My source data is in tabular form. How do I accomplish this workflow?
Accepted Answer
MathWorks Support Team
on 13 Sep 2024 at 0:00
Edited: MathWorks Support Team
on 13 Sep 2024 at 17:47
For ‘mlreportgen.ppt.Table’ objects, one possible way to set number format is to use 'compose' function to format data into multiple strings. Since your source data is in tabular format, you could first use 'table2array' function to convert the tabular data to an array and then use the 'compose' function to convert data to desired number format.
An example is given below:
T = table([1;2;3],[2; 4; 6 ],[3 ; 6 ; 9 ],...
'VariableNames',["One" "Two" "Three"]); % create a table
ArrayTable = table2array(T); % convert tabular data to an array
formatSpec = '%.2f'; % desired number format
StrNum = compose(formatSpec,ArrayTable); % convert data to desired number format
StrTable = [T.Properties.VariableNames; StrNum]; % Obtain the original column header
FormattedTable = table(StrTable); % convert back to table
You can find more information about 'table2array' function and 'compose' function following the links below:
'table2array' function: https://www.mathworks.com/help/matlab/ref/table2array.html
'compose' function: https://www.mathworks.com/help/matlab/ref/string.compose.html
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!