In programmatic report generation how can I display numbers in engineering format inside a table?
5 views (last 30 days)
Show older comments
Hi,
I am trying to programattically create a pdf report. I have to insert a table with numerals and I want to display them in engineering (scientific) format, but it only displays them in double format by default. I tried to use num2str command to convert numbers to strings and put them in a table to see if that works, but that puts an empty table in my report without any values.
Can someone please help me understand how to display numbers in a particular format in a report.
clc
close all
format shortEng
import mlreportgen.report.*;
import mlreportgen.dom.*;
rpt = Report('Table','pdf');
report=mlreportgen.report.Report;
%Add Title Page
ch=Chapter;
ch.Title=strcat('Boom Table Trial');
tp=TitlePage;
tp.Title='Report on Pin Loads';
tp.Subtitle='544L Table';
tp.Author='Varun Vyas';
add(rpt,tp);
add(rpt,TableOfContents)
sec=Section;
sec.Title='Force Table';
ForceTable=load('ForceTable.mat');
ForceVal={(round(ForceTable.boom.Fx,0)),...
(round(ForceTable.boom.Fy,0)),...
(round(ForceTable.boom.Fz,0))};
ForceValTable=table(ForceVal{:,1},ForceVal{:,2},ForceVal{:,3});
ForceValTableRep=BaseTable(ForceValTable);
% ForceValTableRep.Style={Width("100%"),Border("solid"),RowSep("solid"),ColSep("solid")};
% ForceValTableRep.Body.TableEntriesStyle={InnerMargin("2pt","2pt","2pt","2pt"),WhiteSpace("preserve")};
add(sec,ForceValTableRep.Content)
add(ch,sec)
add(rpt,ch)
close(rpt);
rptview(rpt.OutputPath);
Thank You,
Varun Vyas.
0 Comments
Answers (3)
Hari Krishna Ravuri
on 24 Jul 2019
As of now MATLAB is not having the feature of changing the format of numbers in the table generated by the record generator. A workaround for this issue would involve using the "sprintf" and the "arrayfun" function to round off and convert it to a string.Below, a small example on how to use the "sprintf" and "arrayfun" in combination with the report generator in MATLAB is as follows.
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Report('tables');
chapter = Chapter();
chapter.Title = 'Table example';
add(rpt,chapter);
A=[pi pi/2 pi/4;pi/8 pi/16 pi/32;pi/64 pi/128 pi/256];
xStr = arrayfun(@(val) num2str(val, "%.3f"), A, 'UniformOutput',false);
table = BaseTable(xStr);
table.Title = 'Pi Matrix';
add(rpt,table);
delete(gcf);
rptview(rpt);
0 Comments
Eleanor Betton
on 3 Feb 2020
I have been using the above technique for a while but am now getting exclamation marks arount the numbers in my tables, I don't think I got this in versions of Matlab before 2019a.
If you have any better suggestions please do let me know.
function fillCyanResults(rpt)
import mlreportgen.dom.*;
Cyan=[ rpt.Results.nummisaligned(1),rpt.Results.nummissing(1),rpt.Results.Av(1),rpt.Results.StDev(1), rpt.Results.Range(1)];
M = round(Cyan*100)/100;
C = num2cell(M);
fun = @(x) sprintf('%0.2f', x);
Cyan = cellfun(fun, C, 'UniformOutput',0);
bleed_table=array2table(Cyan' ,'VariableNames',{'Cyan'},'RowNames',{'Misaligned nozzles', 'Missing nozzles','Average denisty','Denisty deviation','Density Range'});
table = Table(bleed_table,'Table10');
append(rpt, table);
end
1 Comment
Sampath Rachumallu
on 1 Jul 2020
Try to use 'categorical' to get rid of these exclamation marks around numbers
Sean de Wolski
on 3 Feb 2020
Edited: Sean de Wolski
on 3 Feb 2020
Look at the NumericFormat property of the MATLABVariable class
https://www.mathworks.com/help/rptgen/ug/mlreportgen.report.matlabvariable-class.html
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!