MATLAB: How to insert *.png figures directly into a Report Generator word document

insert imageMATLAB Report Generatorreport generator

I have a folder containing *.png images generated using a different program. I would like to insert these into a word document.
What I have done so far:
Following the "Getting Started" tutorial I imported two classes (or methods?):
import mlreportgen.report.*
import mlreportgen.dom.*
I created a report object:
rpt = Report('C:\reportpath\report','docx');
I created a section with a section title:
sec(1) = Section;
sec(1).Title = ['Test conducted using parameter 1: ' num2str(par(1)) ', and parameter 2: ' par(2)'];
And then I try to insert an image, and this is where I don't understand the documentation. My first attempt was:
fig1 = Image;
Error using mlreportgen.dom.Image
No constructor 'mlreportgen.dom.Image' with matching signature found.
My second attempt was:
fig1 = rptgen.cfr_image;
fig1.Image = srcfig1{7};
Unrecognized property 'Image' for class 'rptgen.cfr_image'.
My latest attempt was:
fig1 = rptgen.cfr_image;
fig1.FileName = 'figure1.png';
As this did not give me an error message I added it to the section / document:
add(sec(1),fig1); % insert figure into section
add(rpt,sec(1));
close(rpt);
What appears in the word document is just the text:
[1×1 rptgen.cfr_image]
Previously I tried using "imread" to read the image into the matlab workspace and then "imagesc" to plot it on a figure, but the quality was very poor. Hence I would like to insert the *.png file directly.

Best Answer

Hi Richard,
Your first attempt, i.e., using the mlreportgen.dom.Image class, was the right way to add an image to the report. The error message states that you should also provide the path of the image file while instantiating an object of this class. See https://www.mathworks.com/help/rptgen/ug/mlreportgen.dom.image-class.html#buik4oj-3
If you also want to have a caption for the image, you can also use mlreportgen.report.FormalImage.
Thanks,
Rahul