Clear Filters
Clear Filters

How do you insert a PNG image with Matlab Report Generator when using a Word Document Template?

38 views (last 30 days)
I'm unable to add images (png... etc) or figures (or even snapshots of figures) into my report made using MATLAB Report Generator and a Microsoft Word Template with 2 'holes' in it. Everything else works fine. Text is able to go into this template without trouble (hole 1) but the image labeled "myfig.png" or img1 won't append or add into the 2nd hole.
import mlreportgen.dom.* %import matlab report generator dom package
import* %import matlab report generator report package
D = Document('FromTemplate','docx','RW_Template_Practice.dotx'); %Name the document D and use a template made in Microsoft Word with 2 "holes"
open(D); %open document D
moveToNextHole(D); %goes to the first hole
append(D,"SN001"); %Text in First hole -- shows up fine in FromTemplate.docx
moveToNextHole(D); %goes to the second hole, made just like the first, but with a different name/tag from first hole
myfig = B_Plot_PWM_CvS_Single_P(345) %runs custom function that returns a 1x1 Figure
saveas(myfig,'myfig','png') %saves my figure as a png image
imgPath = which("myfig.png") %gives full path of image file
img1 = Image(imgPath) %make it into an image object
append(D,img1) %THIS IS WHERE THINGS GO WRONG?? I want to put an image object into the 'hole' but nothing shows up in FromTemplate.docx
close(D); %Document closes normally
rptview(D); %Document opens in Microsoft Word and "myfig.png" or img1 is nowhere to be seen
Rahul Singhal
Rahul Singhal on 3 Aug 2021
Hi Tanya,
Can you share the 'RW_Template_Practice.dotx' template file, which can help us investigate the issue you are facing?
Tanya Mittal
Tanya Mittal on 3 Aug 2021
I've attached the template file I've been struggling with. I noticed that if the template file doesn't have anything except for text in it, everything works fine and the images show up. I don't understand why the template can't have images already in it though.

Sign in to comment.

Accepted Answer

Rahul Singhal
Rahul Singhal on 3 Aug 2021
Hi Tanya,
The issue you are facing is because of the usage of inbuilt cover page in the template file. For the purposes of backward compatibility, Word creates two versions of the hole markup for such pages. To prevent these issues, I would highly recommend you to follow this link on how to fill holes:
Below is a sample on how your modified script will look like:
import mlreportgen.dom.*
D = Document('FromTemplate','docx','RW_Template_Practice.dotx');
% Fill holes in the template
while ~strcmp(D.CurrentHoleId,"#end#")
switch D.CurrentHoleId
case "SerialNumber"
% Append content to "SerialNumber" hole which is the first hole
case "ImbalanceData"
% Append image to "ImbalanceData" hole which is the second hole
myfig = B_Plot_PWM_CvS_Single_P(345);
imgPath = which("myfig.png");
img1 = Image(imgPath);

More Answers (0)


Find more on MATLAB Report Generator in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!