How to print a figure in a dotx template using report generator

조회 수: 18 (최근 30일)
Kenneth Carlson
Kenneth Carlson 2020년 10월 1일
답변: Chunguang LI 2022년 9월 27일
I'm trying to create a report based on a dotx template (word 365) using Report generator. I'm creating the template using the develepor tool in word.
However I'm not able to "print" a figure.
First i tried
Sample = figure('Name','Sample'); plot(0:10,0:10);
import mlreportgen.dom.*; import mlreportgen.report.*;
D = Document('FromTemplate','docx','template.dotx');
open(D);
moveToNextHole(D);
T = Text("This is Sample!");
T.Bold = true;
append(D,T);
% First solution below
MoveToNextHole(D);
append(D,Figure('Sample'));
close(D);
resulting in:
[1x1 mlreportgen.report.Figure]
Second "solution"
I'm able to convert the figure to a picture, but it doesn't scale itself to the template. This causes the image to be far larger than the page and impossible to integrate in report layout. See example below.
moveToNextHole(D);
print(Sample,'-djpeg','Sample');
img = Image('Sample.jpg');
append(D, img);
close(D);
I'm properly using the wrong methods, but for some reason I havent been able to find anything of the subject online. Hopefully someone here can help.
Kind regards

채택된 답변

Rahul Singhal
Rahul Singhal 2020년 10월 1일
Hi Kenneth,
Report Generator provides 2 set of objects: DOM API objects and Report API objects. To know more about them, see https://www.mathworks.com/help/rptgen/ug/what-is-a-reporter.html
You can add both DOM and Reporter objects to a Report API Report, but you can only add DOM API objects to a Document. So, in your first example, in order to add the Figure reporter object, you should use a Report as the container instead of the Document.
Regarding your second solution, you can use mlreportgen.dom.ScaleToFit format to scale this image to fit the page boundaries.
Thanks,
Rahul

추가 답변 (1개)

Chunguang LI
Chunguang LI 2022년 9월 27일
import mlreportgen.report.*
import mlreportgen.dom.*
d=Document('report','docx');
open(d);
plot([0 1 5], [0 0 4])
rpt = Report();
p = Image(getSnapshotImage(Figure(),rpt));
p.Style={HAlign('center'),ScaleToFit};
append(d,p);
close(d);

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by