関数の定義によるレポートの書式設定
R2026bこの例では、ドキュメント オブジェクト モデル (DOM) とレポート API を使用して、レポートを作成および書式設定するコードを記述する方法を示します。カスタム テンプレートを作成したり使用したりしない場合は、スクリプトやカスタム関数を使用して、レポートを生成および書式設定します。スクリプトや関数は、複雑な書式設定やコンテンツ レイアウトを必要としないシンプルなレポートを作成する場合に便利です。レイアウトが複雑なレポートの場合、コンテンツとレイアウトを生成するスクリプトや関数を作成するよりも、カスタム テンプレートを使用する方が効率的で保守性も高くなります。HTML および PDF のテンプレートの作成、Microsoft Word テンプレートの作成、およびMicrosoft Word テンプレートでのホールの追加を参照してください。
この例では、レポートを生成するカスタム関数 rptpop を実行します。rptpop はドキュメントの構成要素を定義し、追加のカスタム関数を使用してテキストとプロットを追加し、コンテンツをレポートに追加します。
カスタム関数の検査
rptpop 関数を開いてコードを確認します。この関数は、HTML、DOCX、または PDF の形式のレポートを作成できます。
はじめに、rptpop は次のコードを使用してレポートを作成し、ヘッダーとスタイルを定義し、レポートにヘッダーを追加します。
function rptpop(doctype) %RPTPOP DOM-based report example % % This example is based on the MATLAB example: "Predicting the US % Population." That example was generated using MATLAB Publish. This % example is generated using the DOM and Report APIs. This example is % intended to illustrate a simple use of the APIs, one that does not % entail use of holes or templates. % % rptpop() generates an HTML version of the report. % % rptpop(doctype) generates a report of the specified type: % "html", "docx", or "pdf". % Copyright MathWorks, 2013-2025 % These statements eliminate the need to qualify the names of DOM and % Report API objects in this function, e.g., you can refer to % mlreportgen.report.Report simply as Report. import mlreportgen.dom.* import mlreportgen.report.* if nargin < 1 doctype = "html"; end % Create a report. rpt = Report("population", doctype); % Create a title for the report. h = Heading(2, "Predicting the US Population"); % Create a border format object that effectively draws a light gray % rule under the title. b = Border(); b.BottomStyle = "single"; b.BottomColor = "LightGray"; b.BottomWidth = "1pt"; % Set the style of the title programmatically to be dark orange with % a light gray rule under it. % % Note that we could have achieved the same effect % by defining a title style in a template associated with this object % and setting the heading's StyleName property to the name of the % template-defined style, e.g., % % h.StyleName = 'Title'; % % This would allow us to change the appearance of the title without % having to change the code. % % Note also that we append the border and color format objects to the % heading's existing Style array. This avoids overwriting the % heading's existing OutlineLevel format. h.Style = [h.Style {Color("DarkOrange"), b}]; % Append the heading to the document. append(rpt, h);
カスタム テンプレートを使用せずにレポートにコンテンツを追加するために、rptpop は addBodyPara と addPlot という 2 つの入れ子関数を定義します。addBodyPara 入れ子関数は、指定された string テキストを段落としてレポートに追加します。
function addBodyPara(rpt, text) % Format report text and append it to a document. import mlreportgen.dom.*; p = Paragraph(text); p.Style = {FontFamily("Arial"), FontSize("10pt")}; append(rpt, p); end
addPlot 関数は、Figure をレポートに追加します。
function addPlot(rpt) % Add a snapshot of the current figure to the report with the Figure % reporter. import mlreportgen.report.*; figReporter = Figure(gcf); % Set image height and width. figReporter.Scaling = "custom"; figReporter.Width = "5in"; figReporter.Height = "4in"; % Append image to document. append(rpt, figReporter); % Delete plot figure window. delete(gcf); end
rptpop は、これらの入れ子関数を使用して、レポートにテキストと Figure を追加します。たとえば、rptpop は次のコードを使用して、レポートの冒頭に 4 つの段落を追加します。
addBodyPara(rpt, "This example shows that using polynomials of " + ... "even modest degree to predict the future by extrapolating " + ... "data is a risky business."); addBodyPara(rpt, "This example is older than MATLAB. It started as " + ... "an exercise in ""Computer Methods for Mathematical " + ... "Computations"", by Forsythe, Malcolm and Moler, published " + ... "by Prentice-Hall in 1977."); addBodyPara(rpt, "Now, MATLAB and Handle Graphics make it " + ... "much easier to vary the parameters and see the results, " + ... "but the underlying mathematical principles are unchanged."); addBodyPara(rpt, "Here is the US Census data from 1900 to 2000.");
rptpop は、株価データを定義し、MATLAB® を使用してプロットを作成し、addPlot 関数を呼び出すことによって、レポートに最初のプロットを追加します。
% Time interval t = (1900:10:2000)'; % Population p = [75.995 91.972 105.711 123.203 131.669 ... 150.697 179.323 203.212 226.505 249.633 281.422]'; % Plot plot(t,p,"bo"); axis([1900 2020 0 400]); title("Population of the U.S. 1900-2000"); ylabel("Millions"); % Add the plot to the document (see addPlot function below). addPlot(rpt);
rptpop は同様のコードを使用して、レポートに段落やプロットを追加します。
rptpop の実行とレポートの表示
レポートの形式タイプを指定するには、rptpop 関数の入力引数に形式タイプを入力します。たとえば、次のコードを実行すると、レポートを HTML ドキュメントとして生成できます。
rptpop("html")他の形式を試すには、関数の入力を "pdf" または "docx" に変更します。
参考
mlreportgen.dom.Document | mlreportgen.dom.Heading | mlreportgen.dom.Border | mlreportgen.dom.Image | mlreportgen.dom.Paragraph