How do you insert a PNG image with Matlab Report Generator when using a Word Document Template?
46 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 mlreportgen.report.* %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 on 3 Aug 2021
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: https://www.mathworks.com/help/rptgen/ug/fill-in-the-blanks-in-a-report-form.html.
Below is a sample on how your modified script will look like:
D = Document('FromTemplate','docx','RW_Template_Practice.dotx');
% Fill holes in the template
% Append content to "SerialNumber" hole which is the first hole
% 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);