メインコンテンツ

getSnapshotImage

Class: mlreportgen.report.Sparkline
Namespace: mlreportgen.report

Get snapshot image path

Since R2026a

Syntax

imgpath = getSnapshotImage(sparkReporter,report)

Description

imgpath = getSnapshotImage(sparkReporter,report) creates an image of the sparkline specified by sparkReporter and returns a path to a file containing the image. Use this method to customize the layout of sparklines in your report.

Input Arguments

expand all

Sparkline reporter object, specified as an mlreportgen.report.Sparkline object.

Report, specified as a mlreportgen.report.Report object.

Output Arguments

expand all

Path of image, returned as a string.

Examples

expand all

This example shows how to add sparklines to a report table.

Import the Report and DOM API namespaces so that you do not have to use long, fully qualified names.

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

Create a table that contains the sales data from January to June for four companies from the list of company names, the sales data, and the names of the months. The rows of the sales data matrix correspond to the companies and the columns correspond to the months.

companyNames = ["Apple", "Amazon", "Microsoft", "Google"];

salesData = [
    38, 56, 50, 2, 45, 65;   % Data for Apple
    43, 78, 53, 72, 48, 75;  % Data for Amazon
    50, 60, 55, 65, 52, 70;  % Data for Microsoft
    55, 70, 60, 75, 57, 80]; % Data for Google

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"];

The "companyNames" array is the first column, followed by columns for each month of sales data. The final column, "Trends", is empty. In this example, you will populate this column with the sparkline for each company.

tbl = table(companyNames', ...
    salesData(:, 1), salesData(:, 2), salesData(:, 3), ...
    salesData(:, 4), salesData(:, 5), salesData(:, 6), ...
    char.empty(4, 0), ...
'VariableNames', ['CompanyName', months, "Trends"]);

Create a new report titled "StockMarketReport" and specify the output format as PDF.

rpt = Report("StockMarketReport","pdf");

Create a MATLABTable reporter object from the table, tbl.

mlTable = MATLABTable(tbl);

Loop through each row in the table and perform the following operations to add sparklines from the sales data of each company.

  • Create a new table entry for the current row.

  • Create a sparkline for the sales data of the current company.

  • Get a snapshot image of the sparkline to include in the report.

  • Create a DOM image object using the captured snapshot image.

  • Append the DOM image to the table entry (this adds the sparkline to the cell).

  • Add the table entry to the corresponding row in the MATLAB table.

for iRow = 1:mlTable.Body.NRows
    tEntry = TableEntry();
    sparkline = Sparkline(salesData(iRow, :));
    image = getSnapshotImage(sparkline, rpt);
    domImage = Image(image);
    append(tEntry, domImage);
    append(mlTable.Body.row(iRow),tEntry);
end

Append the table with sparklines to the report.

append(rpt,mlTable);

Close and view the report.

close(rpt);
rptview(rpt);

Version History

Introduced in R2026a