Main Content

Define a New Reporter

MATLAB®Report Generator™allows you to define a custom reporter based on themlreportgen.report.Reporterclass or a built-in Report API reporter. Base your reporter on a built-in reporter when the built-in reporter meets most of your requirements and you want to rearrange or expand the reporter content. SeeSubclass a Reporter Definition. Base your reporter on themlreportgen.report.Reporterclass to define an entirely new reporter.

To create a reporter based onmlreportgen.report.Reporter, use themlreportgen.report.Reporter.customizeReportermethod. The method creates a skeleton class definition file and makes copies of the default template files for each report output type.

To complete the custom reporter definition:

  • In the template file for each report output type supported by the reporter, define the fixed content and holes for the dynamic content that the reporter generates.

  • In the custom reporter class, define properties that correspond to the template holes.

To use the reporter in a Report API report, create an object of the reporter class, set the property values, and append the object to the report.

Create Class Definition File and Template Copies

Create a class definition file for a custom reporter class and make copies of the default templates by calling themlreportgen.report.Reporter.customizeReportermethod. Provide the path and name of the class to be created as the input argument to the method. To create the reporter class in a class folder, precede the class name with the@character.

For example, this code creates a class fileMyTitlePage.min the folder named@MyTitlePage:

mlreportgen.report.Reporter.customizeReporter("@MyTitlePage")

The class definition fileMyTitlePage.mcontains:

classdefMyTitlePage < mlreportgen.report.Reporterpropertiesendmethodsfunctionobj = MyTitlePage(varargin) obj = obj@mlreportgen.report.Reporter(varargin{:});endendmethods(Hidden)functiontemplatePath = getDefaultTemplatePath(~, rpt) path = MyTitlePage.getClassFolder(); templatePath =...mlreportgen.report.ReportForm.getFormTemplatePath(...path, rpt.Type);endendmethods(Static)function路径= getClassFolder() [path] = fileparts(mfilename('fullpath'));endfunctioncreateTemplate(templatePath, type) path = MyTitlePage.getClassFolder(); mlreportgen.report.ReportForm.createFormTemplate(...templatePath, type, path);endfunctioncustomizeReporter(toClasspath) mlreportgen.report.ReportForm.customizeClass(...toClasspath,"MyTitlePage");endend
The class includes a constructor and the hidden methodgetDefaultTemplatePath. The base reporter class uses thegetDefaultTemplatePathmethod to retrieve theMyTitlePagereporter template that corresponds to the output type of the report to which the reporter is added. For example, if you add the reporter to anmlreportgen.report.Reportobject whose output type is PDF, the base reporter class returns the path of the PDF template that resides in theresources/templates/pdfsubfolder of your reporter definition folder.

Themlreportgen.report.Reporter.customizeReportermethod stores copies of the default template files for each report output type in theresources/templatessubfolder of the folder that contains the class definition file. The paths of the template files relative toresources/templatesare:

  • docx/default.dotx

  • pdf/default.pdftx

  • html/default.htmt

  • html/default.htmtx

For example, the@MyTitlePagefolder has this structure:

@MyTitlePage contains resources, which contains templates. Templates contains html, docx, and pdf, each of which contains the templates files that correspond to the output type.

Define Fixed and Dynamic Content in the Templates

Customize the template files by defining the fixed content and holes for the dynamic content that the custom reporter generates. You only need to customize the template files for the report output types supported by the custom reporter. For example, if the reporter supports only Word reports, customize only thedotxtemplate file.

如果定制的记者需要多个模板, store the templates in a template library in the template file. If the reporter requires only one template, you can store the template content in the body of the template file or in a template in the template library. For example, for theMyTitlePagereporter, which requires only one template, you can add the template fixed content and holes for dynamic content to the body of the template file or to an entry namedMyTitlePagein the template file library.

Define the styles used by the template in the style sheet of the template file.

SeeCreate a Microsoft Word Document Part Template Library,Create a PDF Document Part Template Library, andCreate an HTML Document Part Template Library.

Define Properties and Specify the Template in the Custom Reporter Class

In the custom reporter class:

  • Define a property for each of the holes defined by the 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 the reporter templates define a hole namedTitle, the class definition file must define a property namedTitle.

  • If the reporter uses a template stored in the template library of the template file, add a line to the constructor that sets the propertyTemplateNameto the name of the reporter template. You do not have to specify this property in the class definition file because the customized class inherits this property from the baseslreportgen.report.Reporterclass.

For example, this class definition file definesTitle,Author, andVersionproperties and specifies that the template name isMyTitlePage

classdefMyTitlePage < mlreportgen.report.ReporterpropertiesTitle =""; Author =""; Version ="";endmethodsfunctionobj = MyTitlePage(varargin) obj = obj@mlreportgen.report.Reporter(varargin{:}); obj.TemplateName ="MyTitlePage";endendmethods(Hidden)functiontemplatePath = getDefaultTemplatePath(~, rpt) path = MyTitlePage.getClassFolder(); templatePath =...mlreportgen.report.ReportForm.getFormTemplatePath(...path, rpt.Type);endendmethods(Static)function路径= getClassFolder() [path] = fileparts(mfilename('fullpath'));endfunctioncreateTemplate(templatePath, type) path = MyTitlePage.getClassFolder(); mlreportgen.report.ReportForm.createFormTemplate(...templatePath, type, path);endfunctioncustomizeReporter(toClasspath) mlreportgen.report.ReportForm.customizeClass(...toClasspath,"MyTitlePage");endendend

Use the Custom Reporter

To use your custom reporter:

  1. Create an object of the reporter class.

  2. Set the values of the properties that you defined in the class.

  3. Append the object to a report.

For example:

importmlreportgen.report.*rpt = Report("myreport","pdf"); titlePage = MyTitlePage; titlePage.Title ="My Report"; titlePage.Author ="Me"; titlePage.Version ="1.0"append(rpt,titlePage); close(rpt); rptview(rpt);

See Also

||||

Related Topics