create simulink report generator reports -凯发k8网页登录

main content

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

  1. 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 use blockfinder.

    import slreportgen.report.*
    import slreportgen.finder.*
    import mlreportgen.report.*
  2. load the slrgex_sf_car model.

    model = load_system('slrgex_sf_car');
    
  3. 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 to sdd_ followed by the value of the name 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 .

  4. 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 the title, subtitle, and author 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 the image 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 .

  5. 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 .

  6. 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 its title and content 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 .

  7. 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 the find function of the block finder. the find 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 its title and content properties. set the section title 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 a simulink.report.simulinkobjectproperties reporter. this simulinkobjectproperties 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.

  8. add a chapter for subsystems.

    create a chapter for the subsystems of the model and the blocks in each subsystem.

    ch = chapter("title","subsystems");
  9. 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 a diagram reporter that creates a snapshot of the subsystem diagram.

    sysdiagfinder = systemdiagramfinder(model);
    sysdiagfinder.includeroot = false;

    for more information, see and

  10. 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 finder hasnext and next 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.

  11. 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 .

  12. 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);

see also

网站地图