Define New Types of Reporters

MATLAB® Report Generator™ allows you to define reporter types to meet specialized reporting requirements. For example, if the MATLAB Report Generator TitlePage reporter type does not meet your needs, you can define your own title page reporter type.

Note

The following procedure creates a package of files that define your reporter type. To see how reporter definition packages are structured, subclass a built-in reporter, such as the TitlePage reporter (see Subclass a Reporter Definition). After performing the following steps, the reporter package for your new reporter type will be similar to the subclassed one.

To define a new reporter type:

  1. Create a subfolder for your class definition in the folder containing the report program in which it is to be used. Prefix the name of the class definition folder with @, for example, @MyTitlePage.

    Note

    To use the new class in programs that reside in other folders, add the class definition folder to the MATLAB path.

  2. Create a subfolder named resources in your class definition folder. Create a subfolder named templates in the resources folder.

  3. Create subfolders named pdf, docx, and html in the templates folder. If you do not want to support all output types, create folders only for the types you want to support.

  4. Use mlreportgen.dom.Document.createTemplate to create an empty template named default in each of the resource template folders for the output types you intend to support. For example, to create an empty template in the pdf template folder for your MyTitlePage reporter, enter:

    cd @MyTitlePage/resources/templates/pdf
    mlreportgen.dom.Document.createTemplate('default','pdf);
    

  5. Edit each empty template file to create a template library that contains the templates to be used by your reporter. You must create a template library even if your reporter uses only one template. For example, for your MyTitlePage reporter, add a template named TitlePage to the template library in each of the reporter's template documents. The template documents for PDF, Word, and HTML reports are default.pdftx, default.dotx, and default.htmtx, respectively. In the TitlePage template, define the title page fixed content and holes for the title page dynamic content the title page reporter generates. Define the styles used by the template in the style sheet of the main template.

  6. Using the MATLAB editor, create a class definition (classdef) file for the new reporter type in the class definition folder. The name of the class definition file must be the name of the new reporter type, for example, MyTitlePage.m. The classdef file must define the following:

    • The base class for reporters (mlreportgen.report.Reporter) as the base class for your new type of reporter.

    • A property for each of the holes defined by your templates, including holes in the headers and footers. The property corresponding to a hole must have the same name as the hole. For example, if your reporter template defines a hole named Title, your class definition file must define a property named Title. This code is example of a class definition file.

      classdef MyTitlePage < mlreportgen.report.Reporter
        properties 
          Title = ''
          Author = ''
          Version = '1.0'
        end
      
        methods 
          function obj = MyTitlePage(varargin)
            obj = obj@mlreportgen.report.Reporter(varargin{:});
      
      %  The next line assumes that you have defined a template 
      %  named TitlePage in the template library of the main
      %  template used by this reporter. The base reporter class
      %  fills the holes in this template with the contents of the
      %  corresponding properties defined by your class.
      
            obj.TemplateName = 'MyTitlePage';
          end
        end 
      
        methods (Hidden) 
      % This function is used by the base Reporter class to 
      % retrieve the MyTitlePage reporter template corresponding 
      % to the output type of the report to which the reporter 
      % is added. For example, if you add this reporter to a 
      % Report object whose output type is PDF, the base reporter 
      % class returns the path of the PDF template residing in the
      % resources/templates/pdf directory of your reporter 
      % definition package.
          function templatePath = getDefaultTemplatePath(~,rpt)  
            import mlreportgen.report.*
            path = MyTitlePageTemplate.getClassFolder();
            templatePath = ... 
              ReportForm.getFormTemplatePath(path,rpt.Type); 
          end 
      
        end 
      
        methods (Static)
          function path = getClassFolder()
            [path] = fileparts(mfilename('fullpath')); 
          end 
      
        end  
      
      end
      

Note

This example does not define property value pairs in its constructor. Instantiate the reporter before you set its properties.

This example shows how to use your title page reporter (MyTitlePage), which is defined in the sample classdef file. The example also shows how to set properties after creating an instance of the reporter.

import mlreportgen.report.*
import mlreportgen.dom.*

rpt = Report('myreport','pdf');

tp = MyTitlePage;
tp.Title = 'My Report';
tp.Author = 'Myself';
append(rpt,tp);

close(rpt);
rptview(rpt);

See Also

| | | |

Related Topics