MATLAB Answers

MATLAB report generator table font family

47 views (last 30 days)
Hi, I am using MATLAB R2018b and I want to create a Table in MATLAB report generator and change the font style of its data.
I tried the following code but it does not work. Any ideas?
% import package
import mlreportgen.dom.*
% create the document object
doc_obj = Document('MATLAB_test_report','pdf');
% table
tableObj = Table ( {'a','b';'5','P'} ) ;
tableObj.Width = '30pt';
tableObj.Border = 'none';
tableObj.TableEntriesStyle = {Height('20pt'),FontFamily('Calibri')};
% append
append ( doc_obj , tableObj ) ;
% close document
close(doc_obj);
% display
rptview(doc_obj);

  0 Comments

Sign in to comment.

Accepted Answer

Mary Abbott
Mary Abbott on 18 Mar 2019
Hello,
The code you are using should change the font family of the table entries to Calibri. However, there is a bug in R2018b and previous releases that causes the font family of table entries to be ignored. This bug is fixed in R2019a.
As a workaround for releases prior to R2019a, you can create Paragraph objects for the table entries and set the FontFamilyName property of the objects to 'Calibri'. An example of this is shown below:
% import package
import mlreportgen.dom.*
% create the document object
doc_obj = Document('MATLAB_test_report','pdf');
% Create Paragraph objects for table data
tableData = {'a','b';'5','P'};
for k = 1:numel(tableData)
p = Paragraph(tableData{k});
p.FontFamilyName = 'Calibri';
tableData{k} = p;
end
% table
tableObj = Table ( tableData ) ;
tableObj.Width = '30pt';
tableObj.Border = 'none';
tableObj.TableEntriesStyle = {Height('20pt')};
% append
append ( doc_obj , tableObj ) ;
% close document
close(doc_obj);
% display
rptview(doc_obj);

  1 Comment

Charles Arentzen
Charles Arentzen on 18 Jan 2020
Hey Mary,
Thank you very much for this answer, it was enormously helpful (using 2017b). However, I did run into another issue after following your approach. The font is corrected (and love the fact I can colorize individual table entries!), but for whatever reason the table columns autofit such that the longest entry in the column gets split into 2 lines. I unfortunately cannot show you the report as it is on another network, but hopefully that describes the issue.
Things I have tried that did not work:
  1. added ResizeToFitContents(true) as part of table style following table creation (did nothing)
  2. added whitespace after table entry (if sufficiently long, will force all actual text onto first line but will not eliminate blank second line)
Since the columns in my tables would be much better off being flexibly sized, I was hoping I would be able to read the active column widths following the first iteration of the table, and then tack on a small amount of width for each column.
Any help would be greatly appreciated.

Sign in to comment.

More Answers (0)