Report Generation for Simulink and Stateflow Elements

Finders and reporters reduce the amount of time and complexity required to write code to find and report on Simulink® model elements, such as diagrams and blocks, and on Stateflow® charts and transitions, etc. The Simulink Report Generator™ Report API is a layer on top of the DOM API. Its finders and reporters are based on the Simulink and Stateflow find base class. You specify the container in which to find specific elements, such as blocks in a subsystem or states in a chart. Finder objects return their results in a corresponding array of finder result objects.

The Simulink Report API also includes reporter classes, which return an image of the container. This image is the top level of a model.

Every result object returned by a finder has an associated reporter object, which reports on those results. The reporter object holds the content and formats the content, such as tables of properties and data plots. You add the reporter objects to your reports. Use the MATLAB® Report API reporters to define common report elements. See What Is a Reporter? (MATLAB Report Generator) for information.

All finders and reporters have these features:

  • Default behaviors and values

  • Allow overriding and customizing their output

All finders have find, hasNext, and next methods. The find method finds and returns in an array of result objects all elements for each found element of the specified type. The hasNext and next methods find and return one element at a time and are used to iterate over a list of results. The hasNext method checks whether the container has at least one of the element of the specified type. If the container has one or more of the elements, then the hasNext method queues it for the next method to find and return as a result object.

All reporters have predefined templates. The template for each reporter defines its formatting, layout, and content holes. You do not need to change the template or specify any formats, layouts, or holes unless you want a customized report. You can customize your report by copying and editing its default template or by using a new template. Editing a copy of the default template gives you a starting point and structure to follow to customize your template. Using a new template lets you completely define your template starting with a blank file. To change the order of the report content, reorder the holes in the template. Finders do not use templates. Another way to customize a reporter class is by subclassing it.

The default reporter templates for each output type are in a template library, which is at

matlab\toolbox\shared\slreportgen\rpt\rpt\+slreportgen\
    +report\@<reporter>\resources\templates\<output>
For example, the path to the default template for the DiagramReporter for PDF output is
matlab\toolbox\shared\slreportgen\rpt\rpt\+slreportgen\
    +report\@DiagramReporter\resources\templates\pdf\default.pdftx
For a detailed example of editing a template, see the "Customize a Report API Template" section of What Is a Reporter? (MATLAB Report Generator)

Simulink Report API Classes

The Simulink Report API provides these finder, result, and reporter classes. To use these classes in a report generator program, you must create a container of type slreportgen.report.Report to hold the report.

Finder and Result Classes

Report API ClassDescription
slreportgen.finder.AnnotationFinderFinds Simulink block diagram annotations.
slreportgen.finder.BlockFinderFinds blocks in a Simulink block diagram.
slreportgen.finder.BlockResultContains a block found by a BlockFinder object.
slreportgen.finder.ChartDiagramFinderFinds Stateflow charts in a model.
slreportgen.finder.DiagramElementFinderFinds elements of a Simulink block diagram or Stateflow chart.
slreportgen.finder.DiagramElementResultContains a diagram element found by a DiagramElementFinder object.
slreportgen.finder.DiagramFinderFinds block diagrams and charts in a Simulink model.
slreportgen.finder.DiagramResultContains a diagram found by a DiagramFinder object.
slreportgen.finder.ModelVariableFinderFinds variables used by a Simulink model.
slreportgen.finder.ModelVariableResultContains a model variable found by a ModelVariableFinder object.
slreportgen.finder.StateFinderFinds states in a Stateflow chart.
slreportgen.finder.StateflowDiagramElementFinderFinds elements of a Stateflow chart.
slreportgen.finder.SystemDiagramFinderFinds system block diagrams in a Simulink model.

Reporter Classes

Report API ClassDescription
slreportgen.report.BusObjectReports on Simulink.Bus objects use by a model.
slreportgen.report.DiagramCreates a snapshot of a Simulink block diagram or a Stateflow chart.
slreportgen.report.DocBlock

Reports on a Simulink DocBlock.

slreportgen.report.LookupTableReports on breakpoints and output points of a Simulink lookup table block.
slreportgen.report.MATLABFunction

Reports on a MATLAB Function block or a Stateflow MATLAB function.

slreportgen.report.ModelVariableReports on a model variable.
slreportgen.report.SimulinkObjectPropertiesCreates a table of the properties of a Simulink object.
slreportgen.report.StateflowObjectPropertiesCreates a table of the properties of a Stateflow object.
slreportgen.report.SystemHierarchyCreates a nested list of the subsystems of a Simulink model or subsystem.
slreportgen.report.TruthTableReports on a Simulink truth table block or a Stateflow truth table object.

Find and Report on Blocks in a Model

This example shows how to find and report on all Simulink blocks in the vdp model using the BlockFinder class. The resulting HTML report includes default information and uses default formatting for each block.

  1. Import the Report API package, which let you use class names without including their package names. For example, you can use BlockFinder instead of slreportgen.finder.BlockFinder. In addition to importing the Simulink Report API base classes, import the MATLAB Report API base class. A typical report includes a title page, table of contents, chapters, and sections, which you include as reporter classes in the MATLAB Report API.

    import slreportgen.finder.*
    import slreportgen.report.*
    import mlreportgen.report.*
  2. Load the vdp model.

    model_name = 'vdp';
    load_system(model_name)
  3. Create the container object to hold the report and open the report. In this case, the output report is saved in zipped vdp_model.htmx HTML report. You can use any output name you want. If you run the report generator more than once using the same output file name, the output file is overwritten. To use Simulink Report API finders and reporters in your report generator program, you must use the fully qualified name to create the container object.

    rpt = slreportgen.report.Report('vdp_model','html');
    open(rpt)
  4. Add a chapter and specify its title.

    ch = Chapter('Blocks in Simulink vdp model');
  5. Use the BlockFinder class to create a finder. In this case, the BlockFinder finds all the blocks in the model. Use the find method to find the blocks specified by the finder.

    finder = BlockFinder(model_name); 
    results = find(finder);
    
  6. Loop through the results of the find method and create a section for each block, and add the block property table to the section. Then, add each section to the chapter. After all blocks have been added, add the chapter to the report.

    for result = results
         sect = Section('Title',result.Name);
         add(sect,result)
         add(ch,sect)
    end
    add (rpt,ch);
  7. Close the report and model, and view the report.

    close(rpt);
    close_system(model_name);
    rptview(rpt);

The full program is

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

model_name = 'vdp';
load_system(model_name);

rpt = slreportgen.report.Report('vdp_model','html');
open(rpt)

ch = Chapter('Blocks in Simulink vdp model');
finder = BlockFinder(model_name); 
results = find(finder);
for result = results
     sect = Section('Title',result.Name);
     add(sect,result)
     add(ch,sect);
end
add (rpt,ch);

close(rpt)
close_system(model_name)
rptview(rpt)

The chapter heading and the section headings and property tables of the first two blocks of the resulting report are shown.

Use Specific Finders and Reporters for Different Block Types

Create a PDF report generator that finds all blocks in the sldemo_radar_eml model.

To find all blocks, use the BlockFinder. The if statement shows how to test for MATLAB Function blocks. Use the MATLABFunction reporter to report MATLAB Function block details. The else statement shows how blocks other than MATLAB Function blocks use the BlockFinder find method results.

blkfinder = BlockFinder(model_name);
blks = find(blkfinder);

if slreportgen.utils.isMATLABFunction(blks(i).Object)
     rptr = MATLABFunction(blks(i).Object);
     sec = Section(blks(i).Name);
     add(sec,rptr)
     add(ch,sec)

else
     sec = Section(blks(i).Name);
     add(sec,blks(i)) 
     add(ch,sec)
blkfinder = BlockFinder(model_name);
blks = find(blkfinder);

The full program is

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

model_name = 'sldemo_radar_eml';
load_system(model_name)

rpt = slreportgen.report.Report('radar','pdf');
open(rpt)

blkfinder = BlockFinder(model_name);
blks = find(blkfinder);
ch = Chapter('Blocks in sldemo_radar_eml Model');

for i=1:length(blks)
   if slreportgen.utils.isMATLABFunction(blks(i).Object)
      rptr = MATLABFunction(blks(i).Object);
      sec = Section(blks(i).Name);
      add(sec,rptr)
      add(ch,sec)
   else
      sec = Section(blks(i).Name);
      add(sec,blks(i)) 
      add(ch,sec)
   end
end
add(rpt,ch)

close(rpt)
close_system(model_name)
rptview(rpt)

An example of the information reported for a MATLAB Function block by the MATLABFunction reporter is

An example of the information reported by the find method of the BlockFinder is

Find and Report on Stateflow Elements

This example describes how to find and report on Stateflow states, transitions, and junctions. It reports on the control_logic chart of the sldemo_fuelsys model.

This portion of the code uses the StateFinder and its find method to find and report on states in the chart. It loops through the array of found states and adds each one to the chapter.

stFinder = StateFinder(subsys);
states = find(stFinder);
for state = states
   add(chapter,state)
end
add(rpt,chapter)

To report on the transitions, use the StateflowDiagramElementFinder and its find method. To show the property table with a narrower width than the default, customize the output. First, obtain the reporter for the result. To set the width, use the TableWidth property of the reporter.

chapter = Chapter('Title','Transitions');
trFinder = StateflowDiagramElementFinder...
   ('Container',subsys,'Types','transition');
transitions = find(trFinder);
for transition = transitions
   rptr = transition.getReporter;
   rptr.PropertyTable.TableWidth = '3in'; 
   add(chapter,rptr)
end
add(rpt,chapter)

The complete program is

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

model_name = 'sldemo_fuelsys';
load_system(model_name);
subsys = strcat(model_name,...
   '/fuel_rate_control/control_logic');

rpt = slreportgen.report.Report('output','pdf');
open(rpt)

tp = TitlePage('Title',...
   'Control Logic Chart of sldemo_fuelsys');
add(rpt,tp)
add(rpt,TableOfContents);
   
chapter = Chapter('Title','States');
stFinder = StateFinder(subsys);
states = find(stFinder);
for state = states
   add(chapter,state)
end
add(rpt,chapter)

chapter = Chapter('Title','Transitions');
trFinder = StateflowDiagramElementFinder...
   ('Container',subsys,'Types','transition');
transitions = find(trFinder);
for transition = transitions
   rptr = transition.getReporter;
   rptr.PropertyTable.TableWidth = '3in'; 
   add(chapter,rptr)
end
add(rpt,chapter)

close(rpt)
close_system(model_name)
rptview(rpt)

See Also

Related Topics