Signifcant numbers set in a MATLAB report generator

1 view (last 30 days)
Based on a previous question in this forum (https://www.mathworks.com/matlabcentral/answers/732688-signifcant-numbers-set-in-a-table) I tried it with MATLAB report generator but the output report does not show 2dp.
Code (output report without 2dp):
clc;
clear all;
import mlreportgen.dom.*
xID = ['1';'2';'3';'4';'5'];
xName = {'Sun';'Moon';'Jupiter';'Earth';'Venus'};
I1 = [1.1112;9.2311245;3.341145;6.341122;54.211155];
I2 = [1.1199999;1.1211345;1.881188;1.5781188;1.63711777];
I3 = [6.232114;5.1211223654;4.452112272;3.5711235;2.62112247];
mt = table(xID, xName, I1, I2, I3);
colIsNum = varfun(@isnumeric, mt,'OutputFormat','uniform')
table_data = mt; % keep original, more precise data
table_data{:,colIsNum} = round(mt{:,colIsNum},2)
d = Document('dataInput','pdf');
t = FormalTable(table_data);
t.Style = {Width('100%'),...
Border('solid'),...
ColSep('solid'),...
RowSep('solid')};
t.TableEntriesHAlign = 'center';
append(d,t);
close(d);
rptview(d);
So I was looking on this MATLAB report generator example https://www.mathworks.com/help/rptgen/ug/format_numbers_in_a_table.html but it does not work on tables? Any ideas how to make it work.
Code (with table as input):
clc;
clear all;
import mlreportgen.dom.*
xID = ['1';'2';'3';'4';'5'];
xName = {'Sun';'Moon';'Jupiter';'Earth';'Venus'};
I1 = [1.1112;9.2311245;3.341145;6.341122;54.211155];
I2 = [1.1199999;1.1211345;1.881188;1.5781188;1.63711777];
I3 = [6.232114;5.1211223654;4.452112272;3.5711235;2.62112247];
mt = table(xID, xName, I1, I2, I3);
formattedNumbers = arrayfun(@ (n) sprintf("%1.2f", n),mt);
d = Document('dataInput','pdf');
t = FormalTable(formattedNumbers);
t.Style = {Width('100%'),...
Border('solid'),...
ColSep('solid'),...
RowSep('solid')};
t.TableEntriesHAlign = 'center';
append(d,t);
close(d);
rptview(d);

Answers (1)

Ishu
Ishu on 9 May 2024
In the above code you are attempting to index into a MATLAB table with a single subscript, which MATLAB does not allow for tables. Tables in MATLAB are two-dimensional structures that require row and column subscripts for direct indexing.
It seems you're trying to apply a function across each element of a table using "arrayfun", which does not directly support table inputs in the way you're attempting to use it.
If "mt" is a table and you want to format each number in it to two decimal places, you'll need to handle the table's structure differently. Assuming you want to apply the formatting to every numerical value in the table, you can iterate over each variable in the table and apply the formatting if the variable contains numeric data.
Here's an example of how you might do that:
for varName = mt.Properties.VariableNames
if isnumeric(mt.(varName{1}))
% Apply formatting to each element in the numeric variable
mt.(varName{1}) = arrayfun(@(n) sprintf('%1.2f', n), mt.(varName{1}), 'UniformOutput', false);
end
end
For can refer the below documentation for more information on "arrayfun":

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!