MATLAB Answers

How do I insert *.png figures directly into a Report Generator word document?

54 views (last 30 days)
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.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
What appears in the word document is just the text:
[1x1 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.

Accepted Answer

Rahul Singhal
Rahul Singhal on 20 Jun 2019
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
If you also want to have a caption for the image, you can also use

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!