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

23 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.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:
[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 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
  2 Comments
Richard Frühmann
Richard Frühmann on 21 Jun 2019
Hi Rahul,
If I understand you and the documentation correctly, then after creating the section (in my example) I would create the figure as follows:
fig1 = Image('path\figure1.png');
To generate the report I would then finish with:
add(sec(1),fig1); % insert figure into section
add(rpt,sec(1));
close(rpt);
Unfortunately I am not able to test your solution since I ended up not purchasing the toolbox (post was last year). But it would interest me if I have understood the concept.
:)
Richard

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!