create simulink report generator reports
the simulink® report generator™ report api comprises a set of objects designed to find and format model and simulation data. you can use these objects with matlab® report api and dom api objects to create matlab programs that generate reports on simulink models and simulations. the following example illustrates using the simulink report api and the matlab report api to create a matlab program. this program generates a report on the contents of a simulink model. the report contains these sections:
title page
table of contents
root system chapter — contains the root block diagram and properties of each block in the root diagram
subsystems chapter -- contains the diagram and block properties of each subsystem of the model
stateflow® charts chapter -- contains charts and chart object properties of each chart in the model
import the api functions.
to eliminate the need to use fully qualified names of report, finder, and dom api functions, use these statements. for example, instead of using
slreportgen.finder.blockfinder
, you can useblockfinder
.import slreportgen.report.* import slreportgen.finder.* import mlreportgen.report.*
load the
slrgex_sf_car
model.model = load_system('slrgex_sf_car');
create a report object.
use a simulink report constructor (
slreportgen.report.report
) to create a report object to hold the contents of the report. you must fully qualify the name of the constructor to distinguish it from the matlab report constructor (mlreportgen.report.report
). set the name of the report tosdd_
followed by the value of thename
property of the model.rpt = slreportgen.report.report(['sdd_'... get_param('slrgex_sf_car','name')],'pdf');
to customize properties that apply to the whole report, see .
add a title page.
use a title page reporter constructor (
mlreportgen.report.titlepage
) to create a title page reporter. this reporter generates a title page based on its properties. set thetitle
,subtitle
, andauthor
properties to character arrays that specify the report title, subtitle, and author, respectively.use a diagram reporter constructor (
slreportgen.report.diagram
) to create a diagram reporter for this model. this reporter generates an image of the block diagram of the model. to include this image on the report title page, assign the diagram reporter to theimage
property of the title page reporter. then, add the title page to the report.tp = titlepage; tp.title = upper(get_param(model,'name')); tp.subtitle = 'system design description'; tp.author = 'mathworks'; tp.image = diagram(model); append(rpt,tp);
to customize additional title page properties, see .
add a table of contents.
use a table of contents (toc) reporter constructor to create a toc reporter. this reporter generates a toc for the report. add the toc reporter to the report.
toc = tableofcontents; append(rpt,toc);
to customize the table of contents, see .
add a chapter for the root system.
use a chapter constructor (
mlreportgen.report.chapter
) to create a chapter reporter. this reporter generates a chapter based on itstitle
andcontent
properties. the reporter automatically numbers the chapter title. the chapter reporter also generates the chapter page headers and footers and its page numbers.add a model diagram reporter to the chapter. this reporter returns an image of the block diagram of the model that you add to the chapter.
ch = chapter("title","rootsystem"); append(ch,diagram(model));
for information on customizing chapters, see .
add chapter sections for each root system block.
use the block finder constructor (
slreportgen.report.blockfinder
) to create a block finder for the root diagram. then, use thefind
function of the block finder. thefind
function returns an array of block result objects (slreportgen.report.blockresult
), each of which contains a block.loop through the block result objects. for each result, construct a section reporter (
mlreportgen.report.section
). this reporter generates a numbered report section based on itstitle
andcontent
properties. set the sectiontitle
property to the name of the block on which it reports.add the current block result to the section reporter. adding the result sets the section reporter
content
property to asimulink.report.simulinkobjectproperties
reporter. thissimulinkobjectproperties
reporter generates a table of the properties of the current block, which is then added to the section. add each subsection to the parent chapter. then add the chapter to the report.blkfinder = blockfinder(model); blocks = find(blkfinder); for block = blocks section = section("title", ... strrep(block.name, newline,' ')); append(section,block); append(ch,section); end append(rpt,ch);
for information finding blocks and how to customize sections, see and , respectively.
add a chapter for subsystems.
create a chapter for the subsystems of the model and the blocks in each subsystem.
ch = chapter("title","subsystems");
find subsystem diagrams in the model.
find all subsystem diagrams in the model. the finder returns an array of
diagramresult
objects, each of which contains adiagram
reporter that creates a snapshot of the subsystem diagram.sysdiagfinder = systemdiagramfinder(model); sysdiagfinder.includeroot = false;
for more information, see and
add results to chapter sections.
using loops, create a chapter section for each subsystem. find the blocks and block elements in each subsystem. add a table of block elements to each chapter section and add each section to the chapter. then, add the chapter to the report.
while hasnext(sysdiagfinder) system = next(sysdiagfinder); section1 = section("title",system.name); append(section1,system); blkfinder1 = blockfinder(system); elems = find(blkfinder1); for elem = elems section2 = section("title",... strrep(elem.name,newline,' ')); append(section2,elem); append(section1,section2); end append(ch,section1); end append(rpt,ch);
note
simulink finders can operate in either array or iterator mode. in array mode, use the finder
find
function to return the search results as an array of results. in iterator mode, use the finderhasnext
andnext
functions to return the search results one-by-one. use iterator mode when searching for diagrams in models that have many model references. iterator mode closes a model after compiling and searching it, whereas find mode keeps all the models that it searches open. having many open models can potentially consume all system memory and slow report generation. although the model used in this example does not contain model references, the example uses iterator mode to illustrate its syntax.add a chapter for stateflow charts and objects.
find all stateflow charts in the model. create a chapter. using loops, add subsections for each chart. find all the elements in each chart and add them to the subsections. then, add the section to the chapter and the chapter to the report.
ch = chapter("title", "stateflow charts"); chdiagfinder = chartdiagramfinder(model); while hasnext(chdiagfinder) chart = next(chdiagfinder); section = section("title",chart.name); append(section,chart); objfinder = stateflowdiagramelementfinder(chart); sfobjects = find(objfinder); for sfobj = sfobjects title = sfobj.name; if isempty(title) title = sfobj.type; end objsection = section("title",title); append(objsection,sfobj); append(section,objsection); end append(ch,section); end append(rpt,ch);
for information on chart and diagram element finders, see and .
close the report, run the report, and close the model.
close(rpt); rptview(rpt); close_system(model);
the complete code is:
import slreportgen.report.* import slreportgen.finder.* import mlreportgen.report.* model = load_system('slrgex_sf_car'); rpt = slreportgen.report.report(['sdd_'... get_param('slrgex_sf_car','name')],'pdf'); tp = titlepage; tp.title = upper(get_param(model,'name')); tp.subtitle = 'system design description'; tp.author = 'mathworks'; tp.image = diagram(model); append(rpt,tp); toc = tableofcontents; append(rpt,toc); ch = chapter("title","rootsystem"); append(ch,diagram(model)); blkfinder = blockfinder(model); blocks = find(blkfinder); for block = blocks section = section("title", ... strrep(block.name, newline, ' ')); append(section,block); append(ch,section); end append(rpt,ch); ch = chapter("title","subsystems"); sysdiagfinder = systemdiagramfinder(model); sysdiagfinder.includeroot = false; while hasnext(sysdiagfinder) system = next(sysdiagfinder); section1 = section("title",system.name); append(section1,system); blkfinder1 = blockfinder(system); elems = find(blkfinder1); for elem = elems section2 = section("title",... strrep(elem.name, newline, ' ')); append(section2,elem); append(section1,section2); end append(ch,section1); end append(rpt,ch); ch = chapter("title", "stateflow charts"); chdiagfinder = chartdiagramfinder(model); while hasnext(chdiagfinder) chart = next(chdiagfinder); section = section("title",chart.name); append(section,chart); objfinder = stateflowdiagramelementfinder(chart); sfobjects = find(objfinder); for sfobj = sfobjects title = sfobj.name; if isempty(title) title = sfobj.type; end objsection = section("title",title); append(objsection,sfobj); append(section,objsection); end append(ch,section); end append(rpt,ch); close(rpt); rptview(rpt); close_system(model);