Reporting on DocBlock Blocks

This example shows how to include the contents of Simulink DocBlock blocks in a Microsoft Word report generated by the Report API.

Import the API packages to be used in this report. This allows you to refer to API classes by their unqualified names, that is, without the need to specify the class packages in which they reside.

import mlreportgen.report.*
import slreportgen.report.*
import slreportgen.finder.*
import mlreportgen.dom.*

Load the model included in this example. The model contains DocBlock blocks that contain plain text, HTML, and RTF content, respectively.

model = 'slreportgen_demo_docblock';
load_system(model);

Create a container to hold the report content.

rpt = slreportgen.report.Report('ModelDoc', 'docx');

Add a title page and table of contents.

add(rpt, TitlePage('Title', sprintf('%s Model Documentation', model)));
add(rpt, TableOfContents);

Create a cell array to hold the paths of temporary files used to store the content of DocBlock blocks. Add the paths to this array as the files are created. At the end of the report, use this array to delete the files after the report is completed. The files cannot be deleted until the Word report is generated.

dbFiles = {};

Find and loop through all the systems in the model.

finder = SystemDiagramFinder(model);
for system = find(finder)

Create a chapter for each system. Include the system's name in the chapter title. Use this chapter to to report on the DocBlock content, if any, of the system.

    ch = Chapter('Title', sprintf('System %s', system.Name));

Find and loop through all the DocBlock blocks in the current system.

    docBlockFinder = BlockFinder(system);
    docBlockFinder.Properties = {'MaskType', 'DocBlock'}';
    docBlocks = find(docBlockFinder);
    for docBlock = docBlocks

Use the docblock command to extract the block's content and add it to the chapter.

        contentType = get_param(docBlock.Object, 'DocumentType');        
        switch (contentType)
            case 'RTF'
                dbFile = sprintf('%s.rtf', tempname);
                dbFiles = [dbFiles {dbFile}]; %#ok<AGROW>
                docblock('blk2file',docBlock.Object, dbFile);
                add(ch, DOCXSubDoc(dbFile));
            case 'HTML'
                dbFile = sprintf('%s.html', tempname);
                dbFiles = [dbFiles {dbFile}]; %#ok<AGROW>
                docblock('blk2file',docBlock.Object, dbFile);
                add(ch, HTMLFile(dbFile));
            case 'Text'
                dbFile = sprintf('%s.text', tempname);
                dbFiles = [dbFiles {dbFile}]; %#ok<AGROW>
                docblock('blk2file',docBlock.Object, dbFile);
                add(ch, Paragraph(fileread(dbFile)))
        end
    end
    
    if isempty(docBlocks)
        add(ch, 'This system lacks documentation');
    end
    
    add(rpt, ch);
end

close(rpt);

Use docview to copy linked RTF content into the report, update the report's TOC and page number fields, and close the document, and Word..

docview(rpt.OutputPath, 'unlinkdocxsubdoc', 'updatedocxfields', 'closedoc', 'closeapp');

Only now is it safe to delete the temporary files you used to to store the extracted DocBlock content.

nFiles = numel(dbFiles);
for i = 1:nFiles
    delete(dbFiles{i})
end