MATLAB: How to not include an element in the Table of Content

MATLABMATLAB Report Generatorreport generatorsectiontable

Hello everyone.
I am creating a report and I am using and creating Section objects in loops. So in some cases in may create a lot of of Section. Hence is there a way to not include a Section object in the Table of Content? Or have the same type of object as Section in which I can add Paragraphs, Images, Tables etc..?
Thanks in advance!

Best Answer

Hi Jack,
As mentioned in the answer of the linked question, the TOCObj property of TableOfContents reporter was added in R2018b and hence you are unable to use it in R2018a.
To customize the number of levels in R2018a, you can customize the default template of the TableOfContents reporter to set the number of levels and then use this custom template to create your TableOfContents reporter. You can use mlreportgen.report.TableOfContents.createTemplate to create a copy of the default template and then customize it according to your requirements. As an example, to update the number of levels in PDF template, you will have to update the <toc> field in the dptemplate named TableOfContents as follows:
Change:
<dptemplate name="TableOfContents">
.....
<toc/>
</dptemplate>
to:
<dptemplate name="TableOfContents">
.....
<toc number-of-levels="1"/>
</dptemplate>
If you do not wish to customize the template and wants to achieve this programmatically, another alternative is to first get the DOM implementation of the TableOfContents reporter and then get access to the TOC from its children to customize it. Below is an example for a PDF report:
% Create a PDF report
rpt = mlreportgen.report.Report("myReport","pdf");
open(rpt);
% Create TableOfContents reporter based on the default template
reporter = mlreportgen.report.TableOfContents;
% Get the DOM implementation of this reporter
impl = getImpl(reporter,rpt);
% Get the DOM TOC child, which will be the third one in this case
tocObj = impl.Children(3);
% Customize this object according to your requirements
tocObj.NumberOfLevels = 1;
% Add the implementation to the report
add(rpt,impl);
% Create and add chapter and section objects to the report
% ...

% ...
% Close and view the report
close(rpt);
rptview(rpt);
Both the ways I have shown above demonstrates the TableOfContents customization for PDF reports. It may differ a little if you are generating a HTML or a DOCX report.
Hope this helps!
Thanks,
Rahul