How can I properly specify dimensions of a figure in a report?

14 visualizaciones (últimos 30 días)
Curt Laubscher
Curt Laubscher el 18 de Dic. de 2023
Respondida: nick el 29 de Dic. de 2023
I am trying to automatically generate a report containing a figure using a Word template document. However, any time I have it insert the figure, the size is not what I would like it to be. Below is my sample code. I set the figure size to be 3.5×3 inches, I create an instance of mlreportgen.report.Figure with the source being my figure. I specify Scaling='none' to ensure it doesn't automatically re-scale my specified dimensions. I tried also specifying the Width and Height named arguments, but these seem to have no effect. Either way, the result is a 4.38×3.75 inch figure inserted in the resulting output Word document.
% Create figure with desired size
fig = figure(Color='white',Units='inches');
fig.Position(3:4) = [3.5 3.0]; % Set size
ax = axes(Parent=fig,Units='inches',FontSize=9,Box='on');
ax.YAxis.TickLabelFormat = '%.1f';
axis([0.5 5.5 0 1]);
line(ax,1:5,rand(1,5),LineWidth=1);
% Create report
rpt = MyReport('output_report','docx','MyTemplate.dotx');
rpt.Figure = mlreportgen.report.Figure( ...
Source=fig, ...
Scaling='none', ...
Width=sprintf("%.3f%s",fig.Position(3),"in"), ...
Height=sprintf("%.3f%s",fig.Position(4),"in"));
rpt.fill();
The classdef MyReport is generated using mlreportgen.report.Report.customizeReport with one additional property, Figure, which is required to be a (1,1) mlreportgen.report.Figure. The Word template is a simple template including a single controls field, a Picture Content Control field (with title "Figure"). I attached these documents, along with the resulting document (output_report.docx).
Interestingly, when creating an instance of mlreportgen.report.Figure without specify Scaling='none' whether or not I specify Width and Height, the size of the figure becomes really small. It is unclear what is determining the width and height of the figure and how I can specify it.
How can I properly enforce the correct dimensions of a figure in a report?

Respuestas (1)

nick
nick el 29 de Dic. de 2023
Hi Curt,
I understand from your query that you want help setting the figure size to 3.5x3 inches using a Word template document with the help of MATLAB Report Generator.
To achieve that, you need to set the "Scaling" property to "custom" in mlreportgen.report.Figure as shown below:
rpt.Figure = mlreportgen.report.Figure( ...
Source=fig, ...
Scaling='custom', ...
Width=sprintf("%.3f%s",fig.Position(3),"in"), ...
Height=sprintf("%.3f%s",fig.Position(4),"in"));
The “custom option scales the figure snapshot image based on the values of the Height and Width properties. Here is the snapshot of the obtained output in Word:
Figure 1 Obtained Output of 3.5x3 inches
You may refer the following documentation to learn more about mlreportgen.report.Figure class :
Hope this helps,
Regards,
Neelanshu

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by